156 |
} |
} |
157 |
else |
else |
158 |
{ |
{ |
159 |
|
$this->error=$this->db->error(); |
160 |
return -1; |
return -1; |
161 |
} |
} |
162 |
} |
} |
163 |
else |
else |
164 |
{ |
{ |
165 |
|
$this->error=$this->db->error(); |
166 |
return -2; |
return -2; |
167 |
} |
} |
168 |
} |
} |
169 |
|
else |
170 |
|
{ |
171 |
|
return -3; |
172 |
|
} |
173 |
} |
} |
174 |
|
|
175 |
|
|
291 |
$this->client = $client; |
$this->client = $client; |
292 |
} |
} |
293 |
|
|
294 |
/* |
/** |
295 |
* |
* \brief Supprime une ligne de detail |
296 |
* |
* \param idligne Id de la ligne detail à supprimer |
297 |
*/ |
* \return int >0 si ok, <0 si ko |
298 |
|
*/ |
299 |
function delete_product($idligne) |
function delete_product($idligne) |
300 |
{ |
{ |
301 |
if ($this->statut == 0) |
if ($this->statut == 0) |
302 |
{ |
{ |
303 |
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = $idligne"; |
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = ".$idligne; |
304 |
|
|
305 |
if ($this->db->query($sql) ) |
if ($this->db->query($sql) ) |
306 |
{ |
{ |
307 |
$this->update_price(); |
$this->update_price(); |
308 |
|
|
309 |
return 1; |
return 1; |
310 |
} |
} |
311 |
else |
else |
312 |
{ |
{ |
313 |
return 0; |
return -1; |
314 |
} |
} |
315 |
} |
} |
316 |
|
else |
317 |
|
{ |
318 |
|
return -2; |
319 |
|
} |
320 |
} |
} |
321 |
|
|
322 |
/** |
/** |
361 |
$this->products_qty[$i], |
$this->products_qty[$i], |
362 |
$this->products_remise_percent[$i]); |
$this->products_remise_percent[$i]); |
363 |
} |
} |
364 |
|
|
365 |
/* |
/* |
366 |
* |
* |
367 |
*/ |
*/ |
368 |
|
|
369 |
$this->update_price(); |
$this->update_price(); |
370 |
|
|
371 |
/* |
/* |
372 |
* Affectation au projet |
* Affectation au projet |
373 |
*/ |
*/ |
392 |
return $this->id; |
return $this->id; |
393 |
} |
} |
394 |
|
|
395 |
/** |
/** |
396 |
* \brief Mets à jour le prix total de la proposition |
* \brief Mets à jour le prix total de la proposition |
397 |
* |
* \return int <0 si ko, >0 si ok |
398 |
* |
*/ |
399 |
*/ |
function update_price() |
|
|
|
|
function update_price() |
|
400 |
{ |
{ |
401 |
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; |
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; |
402 |
|
|
403 |
/* |
/* |
404 |
* Liste des produits a ajouter |
* Liste des produits a ajouter |
405 |
*/ |
*/ |
406 |
$sql = "SELECT price, qty, tva_tx FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = $this->id"; |
$sql = "SELECT price, qty, tva_tx FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = $this->id"; |
407 |
if ( $this->db->query($sql) ) |
if ( $this->db->query($sql) ) |
|
{ |
|
|
$num = $this->db->num_rows(); |
|
|
$i = 0; |
|
|
|
|
|
while ($i < $num) |
|
|
{ |
|
|
$obj = $this->db->fetch_object(); |
|
|
$products[$i][0] = $obj->price; |
|
|
$products[$i][1] = $obj->qty; |
|
|
$products[$i][2] = $obj->tva_tx; |
|
|
$i++; |
|
|
} |
|
|
} |
|
|
$calculs = calcul_price($products, $this->remise_percent); |
|
|
|
|
|
$this->remise = $calculs[3]; |
|
|
$this->total_ht = $calculs[0]; |
|
|
$this->total_tva = $calculs[1]; |
|
|
$this->total_ttc = $calculs[2]; |
|
|
/* |
|
|
* |
|
|
*/ |
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET"; |
|
|
$sql .= " price='". ereg_replace(",",".",$this->total_ht)."'"; |
|
|
$sql .= ", tva='". ereg_replace(",",".",$this->total_tva)."'"; |
|
|
$sql .= ", total='". ereg_replace(",",".",$this->total_ttc)."'"; |
|
|
$sql .= ", remise='".ereg_replace(",",".",$this->remise)."'"; |
|
|
$sql .=" WHERE rowid = $this->id"; |
|
|
|
|
|
if ( $this->db->query($sql) ) |
|
|
{ |
|
|
return 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
print "Erreur mise à jour du prix<p>".$sql; |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* \brief Recupère de la base les caractéristiques d'une propale |
|
|
* \param rowid id de la propal à récupérer |
|
|
*/ |
|
|
function fetch($rowid) |
|
|
{ |
|
|
$sql = "SELECT ref,total,price,remise,tva,fk_soc,fk_soc_contact"; |
|
|
$sql .= " ,".$this->db->pdate("datep")."as dp"; |
|
|
$sql .= " ,".$this->db->pdate("fin_validite")."as dfv, model_pdf, note"; |
|
|
$sql .= " , fk_projet, fk_statut, remise_percent, fk_user_author"; |
|
|
$sql .= ", c.label as statut_label"; |
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."propal"; |
|
|
$sql .= "," . MAIN_DB_PREFIX."c_propalst as c"; |
|
|
$sql .= " WHERE fk_statut = c.id"; |
|
|
$sql .= " AND rowid='".$rowid."';"; |
|
|
|
|
|
$resql=$this->db->query($sql); |
|
|
|
|
|
if ($resql) |
|
|
{ |
|
|
if ($this->db->num_rows($resql)) |
|
|
{ |
|
|
$obj = $this->db->fetch_object($resql); |
|
|
|
|
|
$this->id = $rowid; |
|
|
|
|
|
$this->datep = $obj->dp; |
|
|
$this->fin_validite = $obj->dfv; |
|
|
$this->date = $obj->dp; |
|
|
$this->ref = $obj->ref; |
|
|
$this->price = $obj->price; |
|
|
$this->remise = $obj->remise; |
|
|
$this->remise_percent = $obj->remise_percent; |
|
|
$this->total = $obj->total; |
|
|
$this->total_ht = $obj->price; |
|
|
$this->total_tva = $obj->tva; |
|
|
$this->total_ttc = $obj->total; |
|
|
$this->socidp = $obj->fk_soc; |
|
|
$this->soc_id = $obj->fk_soc; |
|
|
$this->projetidp = $obj->fk_projet; |
|
|
$this->contactid = $obj->fk_soc_contact; |
|
|
$this->modelpdf = $obj->model_pdf; |
|
|
$this->note = $obj->note; |
|
|
$this->statut = $obj->fk_statut; |
|
|
$this->statut_libelle = $obj->statut_label; |
|
|
|
|
|
$this->user_author_id = $obj->fk_user_author; |
|
|
|
|
|
if ($obj->fk_statut == 0) |
|
|
{ |
|
|
$this->brouillon = 1; |
|
|
} |
|
|
|
|
|
$this->lignes = array(); |
|
|
$this->db->free($resql); |
|
|
|
|
|
$this->ref_url = '<a href="'.DOL_URL_ROOT.'/comm/propal.php?propalid='.$this->id.'">'.$this->ref.'</a>'; |
|
|
|
|
|
/* |
|
|
* Lignes propales liées à un produit |
|
|
*/ |
|
|
$sql = "SELECT d.description, p.rowid, p.label, p.description as product_desc, p.ref, d.price, d.tva_tx, d.qty, d.remise_percent, d.subprice"; |
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."product as p"; |
|
|
$sql .= " WHERE d.fk_propal = ".$this->id ." AND d.fk_product = p.rowid"; |
|
|
$sql .= " ORDER by d.rowid ASC"; |
|
|
|
|
|
$result = $this->db->query($sql); |
|
|
if ($result) |
|
408 |
{ |
{ |
409 |
$num = $this->db->num_rows($result); |
$num = $this->db->num_rows(); |
410 |
$i = 0; |
$i = 0; |
411 |
|
|
412 |
while ($i < $num) |
while ($i < $num) |
413 |
{ |
{ |
414 |
$objp = $this->db->fetch_object($result); |
$obj = $this->db->fetch_object(); |
415 |
|
$products[$i][0] = $obj->price; |
416 |
$ligne = new PropaleLigne(); |
$products[$i][1] = $obj->qty; |
417 |
$ligne->desc = stripslashes($objp->description); // Description ligne |
$products[$i][2] = $obj->tva_tx; |
|
$ligne->libelle = stripslashes($objp->label); // Label produit |
|
|
$ligne->product_desc = stripslashes($objp->product_desc); // Description produit |
|
|
$ligne->qty = $objp->qty; |
|
|
$ligne->ref = $objp->ref; |
|
|
$ligne->tva_tx = $objp->tva_tx; |
|
|
$ligne->subprice = $objp->subprice; |
|
|
$ligne->remise_percent = $objp->remise_percent; |
|
|
$ligne->price = $objp->price; |
|
|
$ligne->product_id = $objp->rowid; |
|
|
|
|
|
$this->lignes[$i] = $ligne; |
|
|
//dolibarr_syslog("1 ".$ligne->desc); |
|
|
//dolibarr_syslog("2 ".$ligne->product_desc); |
|
418 |
$i++; |
$i++; |
419 |
} |
} |
420 |
$this->db->free($result); |
} |
421 |
|
$calculs = calcul_price($products, $this->remise_percent); |
422 |
|
|
423 |
|
$this->remise = $calculs[3]; |
424 |
|
$this->total_ht = $calculs[0]; |
425 |
|
$this->total_tva = $calculs[1]; |
426 |
|
$this->total_ttc = $calculs[2]; |
427 |
|
|
428 |
|
// Met a jour en base |
429 |
|
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET"; |
430 |
|
$sql .= " price='". ereg_replace(",",".",$this->total_ht)."'"; |
431 |
|
$sql .= ", tva='". ereg_replace(",",".",$this->total_tva)."'"; |
432 |
|
$sql .= ", total='". ereg_replace(",",".",$this->total_ttc)."'"; |
433 |
|
$sql .= ", remise='".ereg_replace(",",".",$this->remise)."'"; |
434 |
|
$sql .=" WHERE rowid = $this->id"; |
435 |
|
|
436 |
|
if ( $this->db->query($sql) ) |
437 |
|
{ |
438 |
|
return 1; |
439 |
} |
} |
440 |
else |
else |
441 |
{ |
{ |
442 |
dolibarr_syslog("Propal::Fetch Erreur lecture des produits"); |
$this->error=$this->db->error(); |
443 |
return -1; |
return -1; |
444 |
} |
} |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
/** |
449 |
|
* \brief Recupère de la base les caractéristiques d'une propale |
450 |
|
* \param rowid id de la propal à récupérer |
451 |
|
*/ |
452 |
|
function fetch($rowid) |
453 |
|
{ |
454 |
|
$sql = "SELECT ref,total,price,remise,tva,fk_soc,fk_soc_contact"; |
455 |
|
$sql .= " ,".$this->db->pdate("datep")."as dp"; |
456 |
|
$sql .= " ,".$this->db->pdate("fin_validite")."as dfv, model_pdf, note"; |
457 |
|
$sql .= " , fk_projet, fk_statut, remise_percent, fk_user_author"; |
458 |
|
$sql .= ", c.label as statut_label"; |
459 |
|
$sql .= " FROM ".MAIN_DB_PREFIX."propal"; |
460 |
|
$sql .= "," . MAIN_DB_PREFIX."c_propalst as c"; |
461 |
|
$sql .= " WHERE fk_statut = c.id"; |
462 |
|
$sql .= " AND rowid='".$rowid."';"; |
463 |
|
|
464 |
/* |
$resql=$this->db->query($sql); |
465 |
* Lignes propales liées à aucun produit |
|
466 |
*/ |
if ($resql) |
467 |
$sql = "SELECT d.qty, d.description, d.price, d.subprice, d.tva_tx, d.rowid, d.remise_percent"; |
{ |
468 |
$sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d"; |
if ($this->db->num_rows($resql)) |
469 |
$sql .= " WHERE d.fk_propal = ".$this->id ." AND d.fk_product = 0"; |
{ |
470 |
|
$obj = $this->db->fetch_object($resql); |
471 |
$result = $this->db->query($sql); |
|
472 |
if ($result) |
$this->id = $rowid; |
473 |
{ |
|
474 |
$num = $this->db->num_rows($result); |
$this->datep = $obj->dp; |
475 |
$j = 0; |
$this->fin_validite = $obj->dfv; |
476 |
|
$this->date = $obj->dp; |
477 |
while ($j < $num) |
$this->ref = $obj->ref; |
478 |
{ |
$this->price = $obj->price; |
479 |
$objp = $this->db->fetch_object($result); |
$this->remise = $obj->remise; |
480 |
$ligne = new PropaleLigne(); |
$this->remise_percent = $obj->remise_percent; |
481 |
$ligne->libelle = stripslashes($objp->description); |
$this->total = $obj->total; |
482 |
$ligne->desc = stripslashes($objp->description); |
$this->total_ht = $obj->price; |
483 |
$ligne->qty = $objp->qty; |
$this->total_tva = $obj->tva; |
484 |
$ligne->ref = $objp->ref; |
$this->total_ttc = $obj->total; |
485 |
$ligne->tva_tx = $objp->tva_tx; |
$this->socidp = $obj->fk_soc; |
486 |
$ligne->subprice = $objp->subprice; |
$this->soc_id = $obj->fk_soc; |
487 |
$ligne->remise_percent = $objp->remise_percent; |
$this->projetidp = $obj->fk_projet; |
488 |
$ligne->price = $objp->price; |
$this->contactid = $obj->fk_soc_contact; |
489 |
$ligne->product_id = 0; |
$this->modelpdf = $obj->model_pdf; |
490 |
|
$this->note = $obj->note; |
491 |
$this->lignes[$i] = $ligne; |
$this->statut = $obj->fk_statut; |
492 |
$i++; |
$this->statut_libelle = $obj->statut_label; |
493 |
$j++; |
|
494 |
} |
$this->user_author_id = $obj->fk_user_author; |
495 |
|
|
496 |
$this->db->free($result); |
if ($obj->fk_statut == 0) |
497 |
} |
{ |
498 |
else |
$this->brouillon = 1; |
499 |
{ |
} |
500 |
dolibarr_syslog("Propal::Fetch Erreur lecture des lignes de propale"); |
|
501 |
|
$this->lignes = array(); |
502 |
return -1; |
$this->db->free($resql); |
503 |
} |
|
504 |
} |
$this->ref_url = '<a href="'.DOL_URL_ROOT.'/comm/propal.php?propalid='.$this->id.'">'.$this->ref.'</a>'; |
505 |
return 1; |
|
506 |
} |
/* |
507 |
else |
* Lignes propales liées à un produit |
508 |
{ |
*/ |
509 |
dolibarr_syslog("Propal::Fetch Erreur lecture de la propale $rowid"); |
$sql = "SELECT d.description, p.rowid, p.label, p.description as product_desc, p.ref, d.price, d.tva_tx, d.qty, d.remise_percent, d.subprice"; |
510 |
return -1; |
$sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."product as p"; |
511 |
} |
$sql .= " WHERE d.fk_propal = ".$this->id ." AND d.fk_product = p.rowid"; |
512 |
} |
$sql .= " ORDER by d.rowid ASC"; |
513 |
|
|
514 |
|
$result = $this->db->query($sql); |
515 |
|
if ($result) |
516 |
|
{ |
517 |
|
$num = $this->db->num_rows($result); |
518 |
|
$i = 0; |
519 |
|
|
520 |
|
while ($i < $num) |
521 |
|
{ |
522 |
|
$objp = $this->db->fetch_object($result); |
523 |
|
|
524 |
|
$ligne = new PropaleLigne(); |
525 |
|
$ligne->desc = stripslashes($objp->description); // Description ligne |
526 |
|
$ligne->libelle = stripslashes($objp->label); // Label produit |
527 |
|
$ligne->product_desc = stripslashes($objp->product_desc); // Description produit |
528 |
|
$ligne->qty = $objp->qty; |
529 |
|
$ligne->ref = $objp->ref; |
530 |
|
$ligne->tva_tx = $objp->tva_tx; |
531 |
|
$ligne->subprice = $objp->subprice; |
532 |
|
$ligne->remise_percent = $objp->remise_percent; |
533 |
|
$ligne->price = $objp->price; |
534 |
|
$ligne->product_id = $objp->rowid; |
535 |
|
|
536 |
|
$this->lignes[$i] = $ligne; |
537 |
|
//dolibarr_syslog("1 ".$ligne->desc); |
538 |
|
//dolibarr_syslog("2 ".$ligne->product_desc); |
539 |
|
$i++; |
540 |
|
} |
541 |
|
$this->db->free($result); |
542 |
|
} |
543 |
|
else |
544 |
|
{ |
545 |
|
dolibarr_syslog("Propal::Fetch Erreur lecture des produits"); |
546 |
|
return -1; |
547 |
|
} |
548 |
|
|
549 |
|
/* |
550 |
|
* Lignes propales liées à aucun produit |
551 |
|
*/ |
552 |
|
$sql = "SELECT d.qty, d.description, d.price, d.subprice, d.tva_tx, d.rowid, d.remise_percent"; |
553 |
|
$sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d"; |
554 |
|
$sql .= " WHERE d.fk_propal = ".$this->id ." AND d.fk_product = 0"; |
555 |
|
|
556 |
|
$result = $this->db->query($sql); |
557 |
|
if ($result) |
558 |
|
{ |
559 |
|
$num = $this->db->num_rows($result); |
560 |
|
$j = 0; |
561 |
|
|
562 |
|
while ($j < $num) |
563 |
|
{ |
564 |
|
$objp = $this->db->fetch_object($result); |
565 |
|
$ligne = new PropaleLigne(); |
566 |
|
$ligne->libelle = stripslashes($objp->description); |
567 |
|
$ligne->desc = stripslashes($objp->description); |
568 |
|
$ligne->qty = $objp->qty; |
569 |
|
$ligne->ref = $objp->ref; |
570 |
|
$ligne->tva_tx = $objp->tva_tx; |
571 |
|
$ligne->subprice = $objp->subprice; |
572 |
|
$ligne->remise_percent = $objp->remise_percent; |
573 |
|
$ligne->price = $objp->price; |
574 |
|
$ligne->product_id = 0; |
575 |
|
|
576 |
|
$this->lignes[$i] = $ligne; |
577 |
|
$i++; |
578 |
|
$j++; |
579 |
|
} |
580 |
|
|
581 |
|
$this->db->free($result); |
582 |
|
} |
583 |
|
else |
584 |
|
{ |
585 |
|
dolibarr_syslog("Propal::Fetch Erreur lecture des lignes de propale"); |
586 |
|
|
587 |
|
return -1; |
588 |
|
} |
589 |
|
} |
590 |
|
return 1; |
591 |
|
} |
592 |
|
else |
593 |
|
{ |
594 |
|
dolibarr_syslog("Propal::Fetch Erreur lecture de la propale $rowid"); |
595 |
|
return -1; |
596 |
|
} |
597 |
|
} |
598 |
|
|
599 |
|
|
600 |
/* |
/* |
601 |
* |
* |
602 |
* |
* |
|
* |
|
603 |
*/ |
*/ |
|
|
|
604 |
function valid($user) |
function valid($user) |
605 |
{ |
{ |
606 |
if ($user->rights->propale->valider) |
if ($user->rights->propale->valider) |