ventes.php 15.3 KB
Newer Older
Ayolo's avatar
Ayolo committed
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
<?php
/* Copyright 2006
 * - Julien Etelain < julien at pmad dot net >
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
$topdir = "../";
require_once($topdir. "include/site.inc.php");
require_once($topdir. "include/cts/sqltable.inc.php");
require_once($topdir. "include/entities/asso.inc.php");

Feu's avatar
FIX    
Feu committed
28
29
30
31
32
33
require_once($topdir. "comptoir/include/typeproduit.inc.php");
require_once($topdir. "comptoir/include/produit.inc.php");
require_once($topdir. "comptoir/include/comptoir.inc.php");



Ayolo's avatar
Ayolo committed
34
35
36
37
38
39
40
41
$site = new site ();
$asso = new asso($site->db,$site->dbrw);

$site->allow_only_logged_users("presentation");

$asso->load_by_id($_REQUEST["id_asso"]);
if ( $asso->id < 1 )
{
42
  $site->error_not_found();
Ayolo's avatar
Ayolo committed
43
44
45
46
  exit();
}

if ( !$site->user->is_in_group("gestion_ae") && !$asso->is_member_role($site->user->id,ROLEASSO_MEMBREBUREAU) )
47
  $site->error_forbidden();
Ayolo's avatar
Ayolo committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

if (isset($_REQUEST['action']) && $_REQUEST['action']=="pdf")
{
  require_once ($topdir . "include/pdf/facture_pdf.inc.php");
  define('FPDF_FONTPATH', $topdir.'./font/');
  $pdf=new FPDF();
  $pdf->AliasNbPages();
  $pdf->SetAutoPageBreak(false);
  $pdf->AddPage();
  $pdf->SetFont('Arial','B',14);

  $pdf->Image($topdir."./images/Ae-blanc.jpg", 10, 10, 75);
  $pdf->SetFont('Times','BI',22);
  $pdf->Cell(0, 18, date("d/m/Y"),'','','R');
  $pdf->Ln();

  $pdf->SetFont('Arial','B',14);
  $pdf->Cell('', 20, "Liste",'','','C');
  $pdf->Ln();

  $pdf->SetFont('Arial','B',11);

  //Header
mbriand's avatar
mbriand committed
71
  $w=array(35,35,50,10,15,15,30);
Ayolo's avatar
Ayolo committed
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

  //Data
  $fill=0;
  $skip=57;
  $height=0;
  $page=1;
  $breakpage = false;

  $conds = array();
  foreach($_POST[conds] as $value)
  {
    $conds[] = $value;
  }

  $req_= new requete($site->db, "SELECT " .
                                "`cpt_debitfacture`.`id_facture`, " .
                                "`cpt_debitfacture`.`date_facture`, " .
                                "`asso`.`id_asso`, " .
                                "`asso`.`nom_asso`, " .
                                "`client`.`prenom_utl` as `nom_client`, " .
                                "`client`.`id_utilisateur` AS `id_utilisateur_client`, " .
                                "`client`.`nom_utl` as `prenom_client`, " .
                                "`vendeur`.`id_utilisateur` AS `id_utilisateur_vendeur`, " .
                                "`cpt_vendu`.`quantite`, " .
                                "`cpt_vendu`.`prix_unit`/100 AS `prix_unit`, " .
                                "`cpt_vendu`.`prix_unit`*`cpt_vendu`.`quantite`/100 AS `total`," .
                                "`cpt_produits`.`prix_achat_prod`*`cpt_vendu`.`quantite`/100 AS `total_coutant`," .
                                "`cpt_comptoir`.`id_comptoir`, " .
                                "`cpt_comptoir`.`nom_cpt`," .
                                "`cpt_produits`.`nom_prod`, " .
                                "`cpt_produits`.`id_produit`, " .
                                "`cpt_type_produit`.`id_typeprod`, " .
                                "`cpt_type_produit`.`nom_typeprod`, " .
                                "`cpt_produits`.`nom_prod` AS `produit`" .
mbriand's avatar
mbriand committed
106
                                "`utl_etu_utbm`.`surnom_utbm` AS `surnom_client` " .
Ayolo's avatar
Ayolo committed
107
108
109
110
111
112
113
                                "FROM `cpt_vendu` " .
                                "INNER JOIN `asso` ON `asso`.`id_asso` =`cpt_vendu`.`id_assocpt` " .
                                "INNER JOIN `cpt_produits` ON `cpt_produits`.`id_produit` =`cpt_vendu`.`id_produit` " .
                                "INNER JOIN `cpt_type_produit` ON `cpt_produits`.`id_typeprod` =`cpt_type_produit`.`id_typeprod` " .
                                "INNER JOIN `cpt_debitfacture` ON `cpt_debitfacture`.`id_facture` =`cpt_vendu`.`id_facture` " .
                                "INNER JOIN `utilisateurs` AS `vendeur` ON `cpt_debitfacture`.`id_utilisateur` =`vendeur`.`id_utilisateur` " .
                                "INNER JOIN `utilisateurs` AS `client` ON `cpt_debitfacture`.`id_utilisateur_client` =`client`.`id_utilisateur` " .
mbriand's avatar
mbriand committed
114
                                "LEFT JOIN `utl_etu_utbm` ON `cpt_debitfacture`.`id_utilisateur_client` =`utl_etu_utbm`.`id_utilisateur` " .
Ayolo's avatar
Ayolo committed
115
116
                                "INNER JOIN `cpt_comptoir` ON `cpt_debitfacture`.`id_comptoir` =`cpt_comptoir`.`id_comptoir` " .
                                "WHERE " .implode(" AND ",$conds).
117
                                "ORDER BY `client`.`nom_utl`, `client`.`prenom_utl`");
118

Ayolo's avatar
Ayolo committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  while ($res = $req_->get_row())
  {
    if(($skip + $height) > 255)
    {
      $breakpage = true;
      $border = 'LRB';
    }
    else if($height == 0)
    {
      //Colors, line width and bold font
      $pdf->SetFillColor(0,0,0);
      $pdf->SetTextColor(255);
      $pdf->SetDrawColor(128,128,128);
      $pdf->SetLineWidth(.3);
      $pdf->SetFont('','B');

      $pdf->Cell($w[0],7,"Nom", 1, 0, 'C', 1);
      $pdf->Cell($w[1],7,"Prenom", 1, 0, 'C', 1);
mbriand's avatar
mbriand committed
137
      $pdf->Cell($w[6],7,"Surnom", 1, 0, 'C', 1);
Ayolo's avatar
Ayolo committed
138
      $pdf->Cell($w[2],7,"Produit", 1, 0, 'C', 1);
mbriand's avatar
mbriand committed
139
      $pdf->Cell($w[3],7,"Qté", 1, 0, 'C', 1);
Ayolo's avatar
Ayolo committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      $pdf->Cell($w[4],7,"Prix", 1, 0, 'C', 1);
      $pdf->Cell($w[5],7,"Total", 1, 0, 'C', 1);

      $pdf->Ln();

      //Color and font restoration
      $pdf->SetFillColor(200,200,200);
      $pdf->SetTextColor(0);
      $pdf->SetFont('');

      $height += 7;

      $border = 'LR';
    }
    else
    {
      $border = 'LR';

      $height += 6;
    }

    $pdf->Cell($w[0],6,utf8_decode($res['nom_client']),$border,0,'L',$fill);
    $pdf->Cell($w[1],6,utf8_decode($res['prenom_client']),$border,0,'L',$fill);
mbriand's avatar
mbriand committed
163
    $pdf->Cell($w[6],6,utf8_decode($res['surnom_client']),$border,0,'L',$fill);
Ayolo's avatar
Ayolo committed
164
165
    $pdf->Cell($w[2],6,utf8_decode($res['produit']),$border,0,'L',$fill);
    $pdf->Cell($w[3],6,utf8_decode($res['quantite']),$border,0,'L',$fill);
mbriand's avatar
mbriand committed
166
167
    $pdf->Cell($w[4],6,number_format(utf8_decode($res['prix_unit']),2),$border,0,'L',$fill);
    $pdf->Cell($w[5],6,number_format(utf8_decode($res['total']),2),$border,0,'L',$fill);
Ayolo's avatar
Ayolo committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    $pdf->Ln();

    if($breakpage == true)
    {
      $skip = 0;
      $height = 0;
      $pdf->Cell(0, 10, "Page " . $page ,0,0,'C');
      $pdf->AddPage();
      $page++;
      $breakpage = false;
    }

    $fill=!$fill;
  }

  $pdf->Cell(array_sum($w),0,'','T');
  $pdf->Output('liste_compta_'.date("d-m-Y").'.pdf',D);
}

$site->start_page("presentation",$asso->nom);

$cts = new contents($asso->get_html_path());
191

Ayolo's avatar
Ayolo committed
192
193
194
195
$cts->add(new tabshead($asso->get_tabs($site->user),"slds"));

$cts->add_title(1,"Ventes cartes AE + e-boutic");

mbriand's avatar
Bleh    
mbriand committed
196
if (! isset($_REQUEST["allprod"]))
mbriand's avatar
Bleh    
mbriand committed
197
198
{
  $allprod = false;
mbriand's avatar
Bleh    
mbriand committed
199
200
201
  $req_produits = new requete($site->db, "SELECT `id_produit`, `nom_prod`
                                          FROM `cpt_produits`
                                          WHERE `cpt_produits`.`id_assocpt` = ".$asso->id."
mbriand's avatar
bleh    
mbriand committed
202
203
                                          AND prod_archive =0
                                          ORDER BY `nom_prod`");
mbriand's avatar
Bleh    
mbriand committed
204
}
mbriand's avatar
fuck    
mbriand committed
205
if (isset($_REQUEST["allprod"]) || ($req_produits->lines <= 0))
mbriand's avatar
Bleh    
mbriand committed
206
207
{
  $allprod = true;
mbriand's avatar
Bleh    
mbriand committed
208
  $req_produits = new requete($site->db, "SELECT `id_produit` , `nom_prod`
mbriand's avatar
bleh    
mbriand committed
209
210
                                          FROM `cpt_produits`
                                          ORDER BY `nom_prod`");
mbriand's avatar
Bleh    
mbriand committed
211
}
mbriand's avatar
Bleh    
mbriand committed
212

mbriand's avatar
bleh    
mbriand committed
213
$produits = array(0=> "(aucun)");
mbriand's avatar
Bleh    
mbriand committed
214
215
216
while($row = $req_produits->get_row())
  $produits[$row['id_produit']] = $row['nom_prod'];

Ayolo's avatar
Ayolo committed
217
218
219
220
221
222
223
$frm = new form ("cptacpt","ventes.php",true,"POST","Critères de selection");
$frm->add_hidden("action","view");
$frm->add_hidden("id_asso",$asso->id);
$frm->add_datetime_field("debut","Date et heure de début");
$frm->add_datetime_field("fin","Date et heure de fin");
$frm->add_entity_select("id_typeprod", "Type", $site->db, "typeproduit",$_REQUEST["id_typeprod"],true);
$frm->add_entity_select("id_comptoir","Lieu", $site->db, "comptoir",$_REQUEST["id_comptoir"],true);
mbriand's avatar
Bleh    
mbriand committed
224
225
$frm->add_select_field("id_produit", "Produit", $produits);

mbriand's avatar
Bleh    
mbriand committed
226
if (! $allprod)
mbriand's avatar
Bleh    
mbriand committed
227
228
  $frm->add_info("<a href=\"?id_asso=".$asso->id."&amp;allprod\">Afficher tous les produits</a>");

229
$frm->add_select_field("a_retirer_vente", "A retirer", array(null => "", 1 => "Non retiré", 2 => "Retiré"));
Ayolo's avatar
Ayolo committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
$frm->add_submit("valid","Voir");
$cts->add($frm,true);


$conds = array("cpt_vendu.id_assocpt='".$asso->id."'");
$comptoir = false;

if ( $_REQUEST["debut"] )
  $conds[] = "cpt_debitfacture.date_facture >= '".date("Y-m-d H:i:s",$_REQUEST["debut"])."'";

if ( $_REQUEST["fin"] )
  $conds[] = "cpt_debitfacture.date_facture <= '".date("Y-m-d H:i:s",$_REQUEST["fin"])."'";

if ( $_REQUEST["id_comptoir"] )
{
  $conds[] = "cpt_debitfacture.id_comptoir='".intval($_REQUEST["id_comptoir"])."'";
  $comptoir=true;
}
248

Ayolo's avatar
Ayolo committed
249
250
if ( $_REQUEST["id_typeprod"] )
  $conds[] = "cpt_produits.id_typeprod='".intval($_REQUEST["id_typeprod"])."'";
251

Ayolo's avatar
Ayolo committed
252
253
254
if ( $_REQUEST["id_produit"] )
  $conds[] = "cpt_vendu.id_produit='".intval($_REQUEST["id_produit"])."'";

255
256
257
258
if ( $_REQUEST["a_retirer_vente"] == '1')
  $conds[] = "cpt_vendu.a_retirer_vente='1'";
if ( $_REQUEST["a_retirer_vente"] == '2')
  $conds[] = "cpt_vendu.a_retirer_vente='0'";
259

Ayolo's avatar
Ayolo committed
260
261
if ( count($conds) >= 1 )
{
262

Ayolo's avatar
Ayolo committed
263
  $req = new requete($site->db, "SELECT " .
264
                                "COUNT(`cpt_vendu`.`id_produit`), " .
Ayolo's avatar
Ayolo committed
265
266
267
268
269
270
271
272
                                "SUM(`cpt_vendu`.`quantite`), " .
                                "SUM(`cpt_vendu`.`prix_unit`*`cpt_vendu`.`quantite`) AS `total`," .
                                "SUM(`cpt_produits`.`prix_achat_prod`*`cpt_vendu`.`quantite`) AS `total_coutant`" .
                                "FROM `cpt_vendu` " .
                                "INNER JOIN `asso` ON `asso`.`id_asso` =`cpt_vendu`.`id_assocpt` " .
                                "INNER JOIN `cpt_produits` ON `cpt_produits`.`id_produit` =`cpt_vendu`.`id_produit` " .
                                "INNER JOIN `cpt_type_produit` ON `cpt_produits`.`id_typeprod` =`cpt_type_produit`.`id_typeprod` " .
                                "INNER JOIN `cpt_debitfacture` ON `cpt_debitfacture`.`id_facture` =`cpt_vendu`.`id_facture` " .
273
                                "INNER JOIN `utilisateurs` AS `vendeur` ON `cpt_debitfacture`.`id_utilisateur` =`vendeur`.`id_utilisateur` " .
Ayolo's avatar
Ayolo committed
274
275
276
                                "INNER JOIN `utilisateurs` AS `client` ON `cpt_debitfacture`.`id_utilisateur_client` =`client`.`id_utilisateur` " .
                                "INNER JOIN `cpt_comptoir` ON `cpt_debitfacture`.`id_comptoir` =`cpt_comptoir`.`id_comptoir` " .
                                "WHERE " .implode(" AND ",$conds).
277
278
                                "ORDER BY `cpt_debitfacture`.`date_facture` DESC");

Ayolo's avatar
Ayolo committed
279
  list($ln,$qte,$sum,$sumcoutant) = $req->get_row();
280
281


Ayolo's avatar
Ayolo committed
282
283
284
285
286
  $cts->add_title(2,"Sommes");
  $cts->add_paragraph("Quantitée : $qte unités<br/>" .
                      "Chiffre d'affaire: ".($sum/100)." Euros<br/>" .
                      "Prix countant total estimé* : ".($sumcoutant/100)." Euros");

Feu's avatar
fix    
Feu committed
287
  $frm = new form ("cptacptpdf","ventes.php?id_asso=".$asso->id,true,"POST","PDF");
Ayolo's avatar
Ayolo committed
288
  $frm->add_hidden("action","pdf");
289
  $i=0;
Ayolo's avatar
Ayolo committed
290
  foreach($conds as $value)
291
292
293
294
  {
    $frm->add_hidden("conds[".$i."]",$value);
    $i++;
  }
Ayolo's avatar
Ayolo committed
295
296
297
298
299
300
  $frm->add_submit("valid","Générer le PDF");
  $cts->add($frm,true);


  if ( $ln < 1000 )
  {
301

Ayolo's avatar
Ayolo committed
302
303
304
305
306
307
308
309
    $req = new requete($site->db, "SELECT " .
                                  "`cpt_debitfacture`.`id_facture`, " .
                                  "`cpt_debitfacture`.`date_facture`, " .
                                  "`asso`.`id_asso`, " .
                                  "`asso`.`nom_asso`, " .
                                  "CONCAT(`client`.`prenom_utl`,' ',`client`.`nom_utl`) as `nom_utilisateur_client`, " .
                                  "`client`.`id_utilisateur` AS `id_utilisateur_client`, " .
                                  "CONCAT(`vendeur`.`prenom_utl`,' ',`vendeur`.`nom_utl`) as `nom_utilisateur_vendeur`, " .
310
                                  "`vendeur`.`id_utilisateur` AS `id_utilisateur_vendeur`, " .
Ayolo's avatar
Ayolo committed
311
312
313
314
315
316
317
318
319
                                  "`cpt_vendu`.`quantite`, " .
                                  "`cpt_vendu`.`prix_unit`/100 AS `prix_unit`, " .
                                  "`cpt_vendu`.`prix_unit`*`cpt_vendu`.`quantite`/100 AS `total`," .
                                  "`cpt_produits`.`prix_achat_prod`*`cpt_vendu`.`quantite`/100 AS `total_coutant`," .
                                  "`cpt_comptoir`.`id_comptoir`, " .
                                  "`cpt_comptoir`.`nom_cpt`," .
                                  "`cpt_produits`.`nom_prod`, " .
                                  "`cpt_produits`.`id_produit`, " .
                                  "`cpt_type_produit`.`id_typeprod`, " .
320
                                  "`cpt_type_produit`.`nom_typeprod`" .
Ayolo's avatar
Ayolo committed
321
322
323
324
325
                                  "FROM `cpt_vendu` " .
                                  "INNER JOIN `asso` ON `asso`.`id_asso` =`cpt_vendu`.`id_assocpt` " .
                                  "INNER JOIN `cpt_produits` ON `cpt_produits`.`id_produit` =`cpt_vendu`.`id_produit` " .
                                  "INNER JOIN `cpt_type_produit` ON `cpt_produits`.`id_typeprod` =`cpt_type_produit`.`id_typeprod` " .
                                  "INNER JOIN `cpt_debitfacture` ON `cpt_debitfacture`.`id_facture` =`cpt_vendu`.`id_facture` " .
326
                                  "INNER JOIN `utilisateurs` AS `vendeur` ON `cpt_debitfacture`.`id_utilisateur` =`vendeur`.`id_utilisateur` " .
Ayolo's avatar
Ayolo committed
327
328
329
330
                                  "INNER JOIN `utilisateurs` AS `client` ON `cpt_debitfacture`.`id_utilisateur_client` =`client`.`id_utilisateur` " .
                                  "INNER JOIN `cpt_comptoir` ON `cpt_debitfacture`.`id_comptoir` =`cpt_comptoir`.`id_comptoir` " .
                                  "WHERE " .implode(" AND ",$conds).
                                  "ORDER BY `cpt_debitfacture`.`date_facture` DESC");
331
332
333
334
335


    $cts->add(new sqltable( "listresp",
                            "Listing", $req, "ventes.php",
                            "id_facture",
Ayolo's avatar
Ayolo committed
336
337
338
339
340
341
342
343
344
345
346
                            array( "id_facture"=>"Facture",
                                   "date_facture"=>"Date",
                                   "nom_typeprod"=>"Type",
                                   "nom_prod"=>"Produit",
                                   "nom_cpt"=>"Lieu",
                                   "nom_utilisateur_vendeur"=>"Vendeur",
                                   "nom_utilisateur_client"=>"Client",
                                   "nom_asso"=>"Asso.",
                                   "quantite"=>"Qte",
                                   "total"=>"Som.",
                                   "total_coutant"=>"Coutant*"),
347
                            array(),
Ayolo's avatar
Ayolo committed
348
349
350
351
352
353
354
355
356
                            array(),
                            array( )
                          ),true);
  }
  $cts->add_paragraph("* ATTENTION: Prix coutant basé sur le prix actuel.");
}


$site->add_contents($cts);
357

Ayolo's avatar
Ayolo committed
358
359
$site->end_page();
?>