359 |
} |
} |
360 |
} |
} |
361 |
|
|
362 |
/** |
/** |
363 |
* Ajoute une ligne de facture |
* Ajoute une ligne de facture |
364 |
*/ |
*/ |
365 |
function addline($facid, $desc, $pu, $qty, $txtva, $fk_product='NULL', $remise_percent=0) |
function addline($facid, $desc, $pu, $qty, $txtva, $fk_product='NULL', $remise_percent=0) |
366 |
{ |
{ |
367 |
if ($this->brouillon) |
if ($this->brouillon) |
368 |
{ |
{ |
369 |
if (strlen(trim($qty))==0) |
if (strlen(trim($qty))==0) |
370 |
{ |
{ |
371 |
$qty=1; |
$qty=1; |
372 |
} |
} |
373 |
$remise = 0; |
$remise = 0; |
374 |
$price = round(ereg_replace(",",".",$pu), 2); |
$price = round(ereg_replace(",",".",$pu), 2); |
375 |
$subprice = $price; |
$subprice = $price; |
376 |
if (trim(strlen($remise_percent)) > 0) |
if (trim(strlen($remise_percent)) > 0) |
377 |
{ |
{ |
378 |
$remise = round(($pu * $remise_percent / 100), 2); |
$remise = round(($pu * $remise_percent / 100), 2); |
379 |
$price = $pu - $remise; |
$price = $pu - $remise; |
380 |
} |
} |
381 |
|
|
382 |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise)"; |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise)"; |
383 |
$sql .= " VALUES ('$facid', '$desc', '$price', '$qty', '$txtva', '$fk_product', '$remise_percent', '$subprice', '$remise') ;"; |
$sql .= " VALUES ('$facid', '$desc'"; |
384 |
|
$sql .= ",".ereg_replace(",",".",$price); |
385 |
if ( $this->db->query( $sql) ) |
$sql .= ",".ereg_replace(",",".",$qty); |
386 |
{ |
$sql .= ",".ereg_replace(",",".",$txtva); |
387 |
$this->updateprice($facid); |
$sql .= ",'$fk_product'"; |
388 |
return 1; |
$sql .= ",'".ereg_replace(",",".",$remise_percent)."'"; |
389 |
} |
$sql .= ",'".ereg_replace(",",".",$subprice)."'"; |
390 |
else |
$sql .= ",'".ereg_replace(",",".",$remise)."') ;"; |
391 |
{ |
|
392 |
print "$sql"; |
if ( $this->db->query( $sql) ) |
393 |
return -1; |
{ |
394 |
} |
$this->updateprice($facid); |
395 |
} |
return 1; |
396 |
} |
} |
397 |
|
else |
398 |
/** |
{ |
399 |
* Mets à jour une ligne de facture |
print "$sql"; |
400 |
*/ |
return -1; |
401 |
function updateline($rowid, $desc, $pu, $qty, $remise_percent=0) |
} |
402 |
{ |
} |
403 |
if ($this->brouillon) |
} |
404 |
{ |
|
405 |
if (strlen(trim($qty))==0) |
/** |
406 |
{ |
* Mets à jour une ligne de facture |
407 |
$qty=1; |
*/ |
408 |
} |
function updateline($rowid, $desc, $pu, $qty, $remise_percent=0) |
409 |
$remise = 0; |
{ |
410 |
$price = round(ereg_replace(",",".",$pu), 2); |
if ($this->brouillon) |
411 |
$subprice = $price; |
{ |
412 |
if (trim(strlen($remise_percent)) > 0) |
if (strlen(trim($qty))==0) |
413 |
{ |
{ |
414 |
$remise = round(($pu * $remise_percent / 100), 2); |
$qty=1; |
415 |
$price = $pu - $remise; |
} |
416 |
} |
$remise = 0; |
417 |
else |
$price = round(ereg_replace(",",".",$pu), 2); |
418 |
{ |
$subprice = $price; |
419 |
$remise_percent=0; |
if (trim(strlen($remise_percent)) > 0) |
420 |
} |
{ |
421 |
|
$remise = round(($pu * $remise_percent / 100), 2); |
422 |
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set description='$desc',price=$price,subprice=$subprice,remise=$remise,remise_percent=$remise_percent,qty=$qty WHERE rowid = $rowid ;"; |
$price = $pu - $remise; |
423 |
$result = $this->db->query( $sql); |
} |
424 |
|
else |
425 |
$this->updateprice($this->id); |
{ |
426 |
} |
$remise_percent=0; |
427 |
} |
} |
428 |
|
|
429 |
/** |
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set description='$desc'"; |
430 |
* Supprime une ligne |
$sql .= ",price=".ereg_replace(",",".",$price); |
431 |
*/ |
$sql .= ",subprice=".ereg_replace(",",".",$subprice); |
432 |
function deleteline($rowid) |
$sql .= ",remise=".ereg_replace(",",".",$remise); |
433 |
{ |
$sql .= ",remise_percent=".ereg_replace(",",".",$remise_percent); |
434 |
if ($this->brouillon) |
$sql .= ",qty=".ereg_replace(",",".",$qty); |
435 |
{ |
$sql .= " WHERE rowid = $rowid ;"; |
436 |
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = $rowid;"; |
|
437 |
$result = $this->db->query( $sql); |
$result = $this->db->query( $sql); |
438 |
|
|
439 |
$this->updateprice($this->id); |
$this->updateprice($this->id); |
440 |
} |
} |
441 |
} |
} |
442 |
|
|
443 |
/** |
/** |
444 |
* Mise à jour des sommes de la facture |
* Supprime une ligne |
445 |
*/ |
*/ |
446 |
function updateprice($facid) |
function deleteline($rowid) |
447 |
{ |
{ |
448 |
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; |
if ($this->brouillon) |
449 |
$err=0; |
{ |
450 |
$sql = "SELECT price, qty, tva_taux FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = $facid;"; |
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = $rowid;"; |
451 |
|
$result = $this->db->query( $sql); |
452 |
$result = $this->db->query($sql); |
|
453 |
|
$this->updateprice($this->id); |
454 |
if ($result) |
} |
455 |
{ |
} |
456 |
$num = $this->db->num_rows(); |
|
457 |
$i = 0; |
/** |
458 |
while ($i < $num) |
* Mise à jour des sommes de la facture |
459 |
{ |
*/ |
460 |
$obj = $this->db->fetch_object($result); |
function updateprice($facid) |
461 |
|
{ |
462 |
$products[$i][0] = $obj->price; |
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; |
463 |
$products[$i][1] = $obj->qty; |
$err=0; |
464 |
$products[$i][2] = $obj->tva_taux; |
$sql = "SELECT price, qty, tva_taux FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = $facid;"; |
465 |
|
|
466 |
$i++; |
$result = $this->db->query($sql); |
467 |
} |
|
468 |
|
if ($result) |
469 |
$this->db->free(); |
{ |
470 |
|
$num = $this->db->num_rows(); |
471 |
$calculs = calcul_price($products, $this->remise_percent); |
$i = 0; |
472 |
|
while ($i < $num) |
473 |
$this->total_remise = $calculs[3]; |
{ |
474 |
$this->amount_ht = $calculs[4]; |
$obj = $this->db->fetch_object($result); |
475 |
$this->total_ht = $calculs[0]; |
|
476 |
$this->total_tva = $calculs[1]; |
$products[$i][0] = $obj->price; |
477 |
$this->total_ttc = $calculs[2]; |
$products[$i][1] = $obj->qty; |
478 |
$tvas = $calculs[5]; |
$products[$i][2] = $obj->tva_taux; |
479 |
|
|
480 |
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET amount = $this->amount_ht, remise=$this->total_remise, total=$this->total_ht, tva=$this->total_tva, total_ttc=$this->total_ttc"; |
$i++; |
481 |
$sql .= " WHERE rowid = $facid ;"; |
} |
482 |
|
|
483 |
if ( $this->db->query($sql) ) |
$this->db->free(); |
484 |
{ |
|
485 |
if ($err == 0) |
$calculs = calcul_price($products, $this->remise_percent); |
486 |
{ |
|
487 |
return 1; |
$this->total_remise = $calculs[3]; |
488 |
} |
$this->amount_ht = $calculs[4]; |
489 |
else |
$this->total_ht = $calculs[0]; |
490 |
{ |
$this->total_tva = $calculs[1]; |
491 |
return -3; |
$this->total_ttc = $calculs[2]; |
492 |
} |
$tvas = $calculs[5]; |
493 |
} |
|
494 |
else |
$sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET "; |
495 |
{ |
$sql .= " amount = ".ereg_replace(",",".",$this->amount_ht); |
496 |
print "$sql<br>"; |
$sql .= ", remise=".ereg_replace(",",".",$this->total_remise); |
497 |
return -2; |
$sql .= ", total=".ereg_replace(",",".",$this->total_ht); |
498 |
} |
$sql .= ", tva=".ereg_replace(",",".",$this->total_tva); |
499 |
} |
$sql .= ", total_ttc=".ereg_replace(",",".",$this->total_ttc); |
500 |
else |
|
501 |
{ |
$sql .= " WHERE rowid = $facid ;"; |
502 |
print "Error"; |
|
503 |
|
if ( $this->db->query($sql) ) |
504 |
|
{ |
505 |
|
if ($err == 0) |
506 |
|
{ |
507 |
|
return 1; |
508 |
|
} |
509 |
|
else |
510 |
|
{ |
511 |
|
return -3; |
512 |
|
} |
513 |
|
} |
514 |
|
else |
515 |
|
{ |
516 |
|
print "$sql<br>"; |
517 |
|
return -2; |
518 |
|
} |
519 |
|
} |
520 |
|
else |
521 |
|
{ |
522 |
|
print "Error"; |
523 |
return -1; |
return -1; |
524 |
} |
} |
525 |
} |
} |
526 |
|
|
527 |
/** |
/** |
528 |
* Applique une remise |
* Applique une remise |
529 |
*/ |
*/ |
530 |
function set_remise($user, $remise) |
function set_remise($user, $remise) |
531 |
{ |
{ |
532 |
if ($user->rights->facture->creer) |
if ($user->rights->facture->creer) |
533 |
{ |
{ |
534 |
|
|
535 |
$this->remise_percent = $remise ; |
$this->remise_percent = $remise ; |
536 |
|
|
537 |
$sql = "UPDATE ".MAIN_DB_PREFIX."facture SET remise_percent = ".ereg_replace(",",".",$remise); |
$sql = "UPDATE ".MAIN_DB_PREFIX."facture SET remise_percent = ".ereg_replace(",",".",$remise); |
538 |
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; |
$sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; |
539 |
|
|
540 |
if ($this->db->query($sql) ) |
if ($this->db->query($sql) ) |
541 |
{ |
{ |
542 |
$this->updateprice($this->id); |
$this->updateprice($this->id); |
543 |
return 1; |
return 1; |
544 |
} |
} |
545 |
else |
else |
546 |
{ |
{ |
547 |
print $this->db->error() . ' in ' . $sql; |
print $this->db->error() . ' in ' . $sql; |
548 |
return -1; |
return -1; |
549 |
} |
} |
550 |
} |
} |
551 |
} |
} |
|
|
|
552 |
} |
} |
|
|
|
553 |
?> |
?> |