1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
| class fatture extends P4A_Base_Mask
{
public $fs_search = null;
public $txt_search = null;
public $cmd_search = null;
public $toolbar = null;
public $table = null;
public $fs_details = null;
public function __construct()
{
parent::__construct();
$p4a = P4A::singleton();
$this->setTitle('Gestione fatture');
// DB Source
$this->build("p4a_db_source", "fatture")
->setTable("fatture")
->addJoin("clienti", "fatture.ID_cliente = clienti.ID",
array('Ragione_sociale'=>'cliente'))
->addOrder("Num_fattura",P4A_ORDER_DESCENDING)
->setPageLimit(5)
->load();
$this->setSource($this->fatture);
$this->firstRow();
//Create detail source. Detail table should be in a different class
$dett_prestazioni = $this->build("prestazioni", "dett_prestazioni");
$dett_prestazioni->source->addFilter("ID_fattura = ?",
$this->fatture->fields->ID);
// Customizing fields properties
$this->setFieldsProperties();
// Search Fieldset
$this->build("p4a_field", "txt_search")
->setLabel("Numero fattura")
->implement("onreturnpress", $this, "search");
$this->build("p4a_button", "cmd_search")
->setLabel("Cerca")
->implement("onClick", $this, "search");
$this->build("p4a_fieldset", "fs_search")
->setLabel("Ricerca per numero fattura")
->setWidth(800)
->anchor($this->txt_search)
->anchorLeft($this->cmd_search);
// Build Tab pane
$tab_pane = $this->build("p4a_tab_pane","tab_pane");
// Toolbar
$this->build("p4a_simple_toolbar", "toolbar")
->setMask($this);
// Hide original print screen button
$this->toolbar->buttons->print->setInvisible();
// Add new button to print pdf
$this->toolbar->addButton('stampafattura','print');
// Intercept onClick and override with our own method
$this->intercept($this->toolbar->buttons->stampafattura,
'onClick','print_doc');
// Join toolbar with this mask
// Table
$this->build("p4a_table", "table")
->setWidth(700)
->setSource($this->fatture)
->setVisibleCols(array("Num_fattura", "Data_fattura", "cliente",
"Pagamento", "Pagata"))
->showNavigationBar();
$this->table->cols->Num_fattura->setLabel("N. fattura");
//Fieldset con l'elenco dei campi
$this->build("p4a_fieldset", "fs_details")
->setLabel("Dati fattura")
->setWidth(700)
->anchor($this->fields->Num_fattura)
->anchorLeft($this->fields->Data_fattura)
->anchor($this->fields->ID_cliente)
->anchorLeft($this->fields->Pagamento)
->anchor($this->fields->Pagata);
$this
->setRequiredField("Num_fattura")
->setRequiredField("Data_fattura");
// events onChange
// Intercept change page on the tab pane
$tab_pane->implement("afterSetActivePage",$this,"ChangePage");
// tabpane settings
$tab_pane->setWidth(750);
$tab1 = $tab_pane->pages->build("p4a_frame","tab1");
$tab2 = $tab_pane->pages->build("p4a_frame","tab2");
$tab1->setLabel("Fatture");
$tab2->setLabel("Dettaglio fattura");
// anchor tab pane
$this->frame->anchor($tab_pane);
// tab1 - Anchor master table
$tab1
->anchor($this->fs_search)
->anchor($this->table)
->anchor($this->fs_details)
->newRow();
// tab2 - Anchor detail table
$tab2
->anchor($dett_prestazioni->toolbar)
->anchor($dett_prestazioni->table)
->anchor($dett_prestazioni->fs_details);
// Display
$this
->display("menu", P4A::singleton()->menu)
->display("top", $this->toolbar)
->setFocus($this->fields->Num_fattura);
}
private function setFieldsProperties()
{
$this->fields->ID_cliente
->setType("select")
->setSource(P4A::singleton()->clienti)
->setSourceDescriptionField("Ragione_sociale")
->setLabel("Cliente")
->setWidth(200);
}
public function search()
{
$value = $this->txt_search->getSQLNewValue();
$this->fatture
->setWhere(P4A_DB::singleton()->getCaseInsensitiveLikeSQL("Num_fattura",
"%{$value}%"))
->firstRow();
if (!$this->fatture->getNumRows()) {
$this->warning("Nessuna fattura trovata");
$this->fatture->setWhere(null);
$this->fatture->firstRow();
}
}
public function ChangePage()
{
$newpage = $this->tab_pane->getActivePageName();
switch ($newpage)
{
case "tab1":
// If display master, enable all toolbar buttons
$this->toolbar->buttons->new->enable();
$this->toolbar->buttons->save->enable();
$this->toolbar->buttons->delete->enable();
$this->toolbar->buttons->print->enable();
// force to redesign toolbar
$this->toolbar->redesign();
break;
case "tab2":
// If display detail, disable some master table buttons
$this->toolbar->buttons->new->disable();
$this->toolbar->buttons->save->disable();
$this->toolbar->buttons->delete->disable();
$this->toolbar->buttons->print->disable();
// force to redesign toolbar
$this->toolbar->redesign();
// Go to first row and fill fieldset
$this->setFocus($this->dett_prestazioni->fields->Descrizione);
$this->dett_prestazioni->firstRow();
break;
}
}
public function print_doc()
// Function to print selected invoice with ezPdf class
{
require("class.ezpdf.php");
$pdf = new Cezpdf("a4","portrait");
// Select font family
$pdf->selectFont($_SERVER["DOCUMENT_ROOT"].P4A_APPLICATION_PATH.
"/libraries/fonts/Helvetica.afm");
// Define some usefull styling tags (like html)
$tmp = array(
"b"=>"Helvetica-Bold.afm"
,"i"=>"Helvetica-Oblique.afm"
,"bi"=>"Helvetica-BoldOblique.afm"
,"ib"=>"Helvetica-BoldOblique.afm"
,"bb"=>"Helvetica-Bold.afm");
$pdf->setFontFamily("Helvetica.afm",$tmp);
$db = p4a_db::singleton();
// Get from db header information
$headerpdf=$db->getAll("SELECT Ragione_sociale, Indirizzo,
CONCAT('Tel: ',Telefono),
CONCAT('Fax: ',Fax),
CONCAT('Email: ',email),
CONCAT('C.F.: ',CF),
CONCAT('P.IVA: ',PIVA),
CONCAT('Banca: ',Dati_bancari)
FROM intestazione");
// Define xy init set up values
$x=65;
$y=820;
$pdf->ezSetY($y);
// Cycle through fields and print header
foreach($headerpdf[0] as $key => $value){
$pdf->ezSetY($y);
$pdf->ezText($value,10,array("aleft"=>$x,"justification"=>"left"));
$y-=10;
}
// Retrieve actual customer's id
$actualIdCliente = $this->fatture->fields->ID_cliente->getNewValue();
// Get customer's data from db
$invoiceData = $db->getAll("SELECT Ragione_sociale, indirizzo,
CONCAT_WS(' - ', CAP, Citta, Provincia),
PIVA, CF
FROM clienti
WHERE ID = '$actualIdCliente'");
$y-=20;
$pdf->ezSetY($y);
$pdf->ezText("Spett.le ",10,array('aleft'=>'300',
'justification'=>'left'));
$x=350;
// Cycle through fields and print data
foreach($invoiceData[0] as $key => $value){
$pdf->ezSetY($y);
$pdf->ezText($value,10,array('aleft'=>$x,'justification'=>'left'));
$y-=10;
}
// Print invoice number and date (we have it!)
$numDateInvoice = "Fattura n. <b>".
$this->fields->Num_fattura->getValue().
"</b> del <b><i>".
$this->fields->Data_fattura->getNewValue().
"</i></b>";
$x=65;
$y-=20;
$pdf->ezSetY($y);
$pdf->ezText($numDateInvoice,10,array('aleft'=>$x,
'justification'=>'left'));
// Retrieve actual invoice id
$actualIdFattura = $this->fatture->fields->ID->getNewValue();
// Get invoice detail items from db
$detInvoiceData = $db->getAll("SELECT Descrizione, Importo,
CONCAT(Aliquota_IVA,'%')
FROM prestazioni
WHERE ID_Fattura = '$actualIdFattura'");
// Print header fields name
$y-=30;
$pdf->ezSetY($y);
$pdf->ezText("Descrizione",12,array('aleft'=>'65',
'justification'=>'left'));
$pdf->ezSetY($y);
$pdf->ezText("Importo",12,array('aleft'=>'415', 'aright'=>'485',
'justification'=>'right'));
$pdf->ezSetY($y);
$pdf->ezText("IVA",12,array('aleft'=>'515',
'justification'=>'left'));
$y-=15;
$pdf->line(65,$y,540,$y);
$x=65;
$y-=10;
// Cycle through records and fields and print data
foreach($detInvoiceData as $row){
foreach($row as $key => $value){
$pdf->ezSetY($y);
if($key=="Importo"){
//format price with 2 decimals
$formattedValue = sprintf("%01.2f", $value);
$pdf->ezText($formattedValue,10,array('aleft'=>$x,
'aright'=>$x+70,'justification'=>'right'));
}
else{
$pdf->ezText($value,10,array('aleft'=>$x,
'justification'=>'left'));
}
if($x==65)
$x+=350;
else
$x+=100;
}
$x=65;
$y-=10;
}
// Get total amount price and tax with an aggregation query
$totPriceInvoice = $db->getAll( "SELECT fatture.ID,
SUM( prestazioni.importo) AS TotaleImponibile,
SUM( prestazioni.importo * (prestazioni.Aliquota_IVA /100)) AS TotaleIVA,
SUM(prestazioni.importo * (1 + (prestazioni.Aliquota_IVA /100))) AS Totale
FROM fatture
RIGHT JOIN prestazioni ON fatture.ID = prestazioni.ID_fattura
GROUP BY fatture.ID HAVING fatture.ID='$actualIdFattura'");
$y-=25;
$pdf->line(65,$y,540,$y);
// Print a summary with total price without tax, total tax,
// and total price including tax.
$y-=50;
$pdf->ezSetY($y);
$pdf->ezText('<b>Totale imponibile</b>',
10,array('aleft'=>'300',
'justification'=>'left'));
$pdf->ezSetY($y);
$formattedValue = sprintf("%01.2f",$totPriceInvoice[0]['TotaleImponibile']);
$pdf->ezText("<b>".$formattedValue."</b>",10,
array('aleft'=>'415','aright'=>'485',
'justification'=>'right'));
$y-=12;
$pdf->ezSetY($y);
$pdf->ezText('<b>Totale IVA</b>',10,array('aleft'=>'300',
'justification'=>'left'));
$pdf->ezSetY($y);
$formattedValue = sprintf("%01.2f",$totPriceInvoice[0]['TotaleIVA']);
$pdf->ezText("<b>".$formattedValue."</b>",10,
array('aleft'=>'415','aright'=>'485',
'justification'=>'right'));
$y-=12;
$pdf->ezSetY($y);
$pdf->ezText('<b>Totale fattura</b>',10,
array('aleft'=>'300',
'justification'=>'left'));
$pdf->ezSetY($y);
$formattedValue = sprintf("%01.2f",$totPriceInvoice[0]['Totale']);
$pdf->ezText("<b>".$formattedValue."</b>",10,
array('aleft'=>'415','aright'=>'485',
'justification'=>'right'));
// Send pdf to file
$filename = 'fattura.pdf';
$pdfcode = $pdf->output(1); // data stream without header
P4A_Output_File($pdfcode,$filename);
}
} |