41 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
42 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobjectline.class.php";
43 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
44 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
45 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
46 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
47 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonincoterm.class.php';
64 public $element =
'propal';
69 public $table_element =
'propal';
74 public $table_element_line =
'propaldet';
79 public $fk_element =
'fk_propal';
84 public $picto =
'propal';
90 public $ismultientitymanaged = 1;
96 public $restrictiononfksoc = 1;
145 public $date_creation;
156 public $date_validation;
161 public $date_signature;
166 public $user_signature;
183 public $date_livraison;
188 public $delivery_date;
191 public $fin_validite;
193 public $user_author_id;
194 public $user_valid_id;
195 public $user_close_id;
213 public $cond_reglement_code;
214 public $deposit_percent;
215 public $mode_reglement_code;
216 public $remise_percent;
233 public $address_type;
236 public $availability_id;
237 public $availability_code;
239 public $duree_validite;
241 public $demand_reason_id;
242 public $demand_reason_code;
244 public $warehouse_id;
246 public $extraparams = array();
251 public $lines = array();
254 public $labelStatus = array();
255 public $labelStatusShort = array();
261 public $fk_multicurrency;
263 public $multicurrency_code;
264 public $multicurrency_tx;
265 public $multicurrency_total_ht;
266 public $multicurrency_total_tva;
267 public $multicurrency_total_ttc;
298 public $fields = array(
299 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
300 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>15,
'index'=>1),
301 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>20),
302 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>22),
303 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefExt',
'enabled'=>1,
'visible'=>0,
'position'=>40),
304 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'position'=>23),
305 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Fk projet',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>24),
306 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>25),
307 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>55),
308 'datep' =>array(
'type'=>
'date',
'label'=>
'Date',
'enabled'=>1,
'visible'=>-1,
'position'=>60),
309 'fin_validite' =>array(
'type'=>
'datetime',
'label'=>
'DateEnd',
'enabled'=>1,
'visible'=>-1,
'position'=>65),
310 'date_valid' =>array(
'type'=>
'datetime',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>70),
311 'date_cloture' =>array(
'type'=>
'datetime',
'label'=>
'DateClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
312 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'Fk user author',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
313 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>85),
314 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
315 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'Fk user cloture',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
316 'price' =>array(
'type'=>
'double',
'label'=>
'Price',
'enabled'=>1,
'visible'=>-1,
'position'=>105),
317 'remise_percent' =>array(
'type'=>
'double',
'label'=>
'RelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>110),
320 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalHT',
'enabled'=>1,
'visible'=>-1,
'position'=>125,
'isameasure'=>1),
321 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'VAT',
'enabled'=>1,
'visible'=>-1,
'position'=>130,
'isameasure'=>1),
322 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax1',
'enabled'=>1,
'visible'=>-1,
'position'=>135,
'isameasure'=>1),
323 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LocalTax2',
'enabled'=>1,
'visible'=>-1,
'position'=>140,
'isameasure'=>1),
324 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'TotalTTC',
'enabled'=>1,
'visible'=>-1,
'position'=>145,
'isameasure'=>1),
325 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'BankAccount',
'enabled'=>
'$conf->banque->enabled',
'visible'=>-1,
'position'=>150),
326 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'Currency',
'enabled'=>1,
'visible'=>-1,
'position'=>155),
327 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'position'=>160),
328 'deposit_percent' =>array(
'type'=>
'varchar(63)',
'label'=>
'DepositPercent',
'enabled'=>1,
'visible'=>-1,
'position'=>161),
329 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>165),
330 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>170),
331 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>175),
332 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'PDFTemplate',
'enabled'=>1,
'visible'=>0,
'position'=>180),
333 'date_livraison' =>array(
'type'=>
'date',
'label'=>
'DateDeliveryPlanned',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
334 'fk_shipping_method' =>array(
'type'=>
'integer',
'label'=>
'ShippingMethod',
'enabled'=>1,
'visible'=>-1,
'position'=>190),
335 'fk_warehouse' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Fk warehouse',
'enabled'=>
'$conf->stock->enabled',
'visible'=>-1,
'position'=>191),
336 'fk_availability' =>array(
'type'=>
'integer',
'label'=>
'Availability',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
337 'fk_delivery_address' =>array(
'type'=>
'integer',
'label'=>
'DeliveryAddress',
'enabled'=>1,
'visible'=>0,
'position'=>200),
338 'fk_input_reason' =>array(
'type'=>
'integer',
'label'=>
'InputReason',
'enabled'=>1,
'visible'=>-1,
'position'=>205),
339 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>215),
340 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>220),
341 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>225),
342 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'MulticurrencyID',
'enabled'=>1,
'visible'=>-1,
'position'=>230),
343 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'MulticurrencyCurrency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>235),
344 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>240,
'isameasure'=>1),
345 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>245,
'isameasure'=>1),
346 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>250,
'isameasure'=>1),
347 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>255,
'isameasure'=>1),
348 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>260),
349 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>500),
350 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>900),
385 global $conf, $langs;
389 $this->socid = $socid;
390 $this->
id = $propalid;
392 $this->duree_validite =
getDolGlobalInt(
'PROPALE_VALIDITY_DURATION', 0);
408 public function add_product($idproduct, $qty, $remise_percent = 0)
411 global $conf, $mysoc;
417 dol_syslog(get_class($this).
"::add_product $idproduct, $qty, $remise_percent");
418 if ($idproduct > 0) {
420 $prod->fetch($idproduct);
422 $productdesc = $prod->description;
426 if (empty($tva_tx)) {
431 $localtax1_tx =
get_localtax($tva_tx, 1, $mysoc, $this->thirdparty, $tva_npr);
432 $localtax2_tx =
get_localtax($tva_tx, 2, $mysoc, $this->thirdparty, $tva_npr);
435 if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
436 $price = $prod->multiprices[$this->thirdparty->price_level];
443 $line->fk_product = $idproduct;
444 $line->desc = $productdesc;
447 $line->remise_percent = $remise_percent;
448 $line->vat_src_code = $vat_src_code;
449 $line->tva_tx = $tva_tx;
450 $line->fk_unit = $prod->fk_unit;
452 $line->info_bits = 1;
455 $this->lines[] = $line;
471 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
472 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
477 $result =
$remise->fetch($idremise);
481 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
482 $this->
db->rollback();
488 $this->line->context = $this->context;
490 $line->fk_propal = $this->id;
491 $line->fk_remise_except =
$remise->id;
492 $line->desc =
$remise->description;
493 $line->vat_src_code =
$remise->vat_src_code;
494 $line->tva_tx =
$remise->tva_tx;
495 $line->subprice = -
$remise->amount_ht;
496 $line->fk_product = 0;
498 $line->remise_percent = 0;
500 $line->info_bits = 2;
503 $line->price = -
$remise->amount_ht;
505 $line->total_ht = -
$remise->amount_ht;
506 $line->total_tva = -
$remise->amount_tva;
507 $line->total_ttc = -
$remise->amount_ttc;
509 $result = $line->insert();
516 $this->
db->rollback();
520 $this->error = $line->error;
521 $this->errors = $line->errors;
522 $this->
db->rollback();
526 $this->
db->rollback();
568 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $fk_product = 0, $remise_percent = 0.0, $price_base_type =
'HT', $pu_ttc = 0.0, $info_bits = 0, $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = 0, $pa_ht = 0, $label =
'', $date_start =
'', $date_end =
'', $array_options = 0, $fk_unit =
null, $origin =
'', $origin_id = 0, $pu_ht_devise = 0, $fk_remise_except = 0, $noupdateafterinsertline = 0)
570 global $mysoc, $conf, $langs;
572 dol_syslog(get_class($this).
"::addline propalid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type, fk_remise_except=".$fk_remise_except);
574 if ($this->statut == self::STATUS_DRAFT) {
575 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
578 if (empty($remise_percent)) {
584 if (empty($info_bits)) {
590 if (empty($fk_parent_line) || $fk_parent_line < 0) {
594 $remise_percent =
price2num($remise_percent);
597 $pu_ht_devise =
price2num($pu_ht_devise);
599 if (!preg_match(
'/\((.*)\)/', $txtva)) {
605 if ($price_base_type ==
'HT') {
616 if ($date_start && $date_end && $date_start > $date_end) {
617 $langs->load(
"errors");
618 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
624 $product_type = $type;
625 if (!empty($fk_product) && $fk_product > 0) {
627 $result = $product->fetch($fk_product);
628 $product_type = $product->type;
630 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_PROPOSAL) && $product_type == 0 && $product->stock_reel < $qty) {
631 $langs->load(
"errors");
632 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnProposal', $product->ref);
633 $this->
db->rollback();
649 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
650 $vat_src_code = $reg[1];
651 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
654 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
656 $total_ht = $tabprice[0];
657 $total_tva = $tabprice[1];
658 $total_ttc = $tabprice[2];
659 $total_localtax1 = $tabprice[9];
660 $total_localtax2 = $tabprice[10];
661 $pu_ht = $tabprice[3];
662 $pu_tva = $tabprice[4];
663 $pu_ttc = $tabprice[5];
666 $multicurrency_total_ht = $tabprice[16];
667 $multicurrency_total_tva = $tabprice[17];
668 $multicurrency_total_ttc = $tabprice[18];
669 $pu_ht_devise = $tabprice[19];
673 if ($ranktouse == -1) {
674 $rangmax = $this->
line_max($fk_parent_line);
675 $ranktouse = $rangmax + 1;
682 if ($remise_percent > 0) {
683 $remise = round(($pu * $remise_percent / 100), 2);
690 $this->line->context = $this->context;
692 $this->line->fk_propal = $this->id;
693 $this->line->label = $label;
694 $this->line->desc = $desc;
695 $this->line->qty = $qty;
697 $this->line->vat_src_code = $vat_src_code;
698 $this->line->tva_tx = $txtva;
699 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
700 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
701 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
702 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
703 $this->line->fk_product = $fk_product;
704 $this->line->product_type = $type;
705 $this->line->fk_remise_except = $fk_remise_except;
706 $this->line->remise_percent = $remise_percent;
707 $this->line->subprice = $pu_ht;
708 $this->line->rang = $ranktouse;
709 $this->line->info_bits = $info_bits;
710 $this->line->total_ht = $total_ht;
711 $this->line->total_tva = $total_tva;
712 $this->line->total_localtax1 = $total_localtax1;
713 $this->line->total_localtax2 = $total_localtax2;
714 $this->line->total_ttc = $total_ttc;
715 $this->line->special_code = $special_code;
716 $this->line->fk_parent_line = $fk_parent_line;
717 $this->line->fk_unit = $fk_unit;
719 $this->line->date_start = $date_start;
720 $this->line->date_end = $date_end;
722 $this->line->fk_fournprice = $fk_fournprice;
723 $this->line->pa_ht = $pa_ht;
725 $this->line->origin_id = $origin_id;
726 $this->line->origin = $origin;
729 $this->line->fk_multicurrency = $this->fk_multicurrency;
730 $this->line->multicurrency_code = $this->multicurrency_code;
731 $this->line->multicurrency_subprice = $pu_ht_devise;
732 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
733 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
734 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
737 if (empty($qty) && empty($special_code)) {
738 $this->line->special_code = 3;
742 $this->line->price =
$price;
744 if (is_array($array_options) && count($array_options) > 0) {
745 $this->line->array_options = $array_options;
748 $result = $this->line->insert();
751 if (!empty($fk_parent_line)) {
753 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
754 $linecount = count($this->lines);
755 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
761 if (empty($noupdateafterinsertline)) {
767 return $this->line->id;
769 $this->error = $this->
db->error();
770 $this->
db->rollback();
774 $this->error = $this->line->error;
775 $this->errors = $this->line->errors;
776 $this->
db->rollback();
780 dol_syslog(get_class($this).
"::addline status of proposal must be Draft to allow use of ->addline()", LOG_ERR);
815 public function updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $desc =
'', $price_base_type =
'HT', $info_bits = 0, $special_code = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice = 0, $pa_ht = 0, $label =
'', $type = 0, $date_start =
'', $date_end =
'', $array_options = 0, $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $rang = 0)
817 global $mysoc, $langs;
819 dol_syslog(get_class($this).
"::updateLine rowid=$rowid, pu=$pu, qty=$qty, remise_percent=$remise_percent,
820 txtva=$txtva, desc=$desc, price_base_type=$price_base_type, info_bits=$info_bits, special_code=$special_code, fk_parent_line=$fk_parent_line, pa_ht=$pa_ht, type=$type, date_start=$date_start, date_end=$date_end");
821 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
824 $remise_percent =
price2num($remise_percent);
827 $pu_ht_devise =
price2num($pu_ht_devise);
828 if (!preg_match(
'/\((.*)\)/', $txtva)) {
834 if (empty($qty) && empty($special_code)) {
837 if (!empty($qty) && $special_code == 3) {
844 if ($date_start && $date_end && $date_start > $date_end) {
845 $langs->load(
"errors");
846 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
850 if ($this->statut == self::STATUS_DRAFT) {
863 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
864 $vat_src_code = $reg[1];
865 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
870 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
871 $total_ht = $tabprice[0];
872 $total_tva = $tabprice[1];
873 $total_ttc = $tabprice[2];
874 $total_localtax1 = $tabprice[9];
875 $total_localtax2 = $tabprice[10];
876 $pu_ht = $tabprice[3];
877 $pu_tva = $tabprice[4];
878 $pu_ttc = $tabprice[5];
881 $multicurrency_total_ht = $tabprice[16];
882 $multicurrency_total_tva = $tabprice[17];
883 $multicurrency_total_ttc = $tabprice[18];
884 $pu_ht_devise = $tabprice[19];
889 if ($remise_percent > 0) {
890 $remise = round(($pu * $remise_percent / 100), 2);
896 $line->fetch($rowid);
898 $staticline = clone $line;
900 $line->oldline = $staticline;
902 $this->line->context = $this->context;
903 $this->line->rang = $rang;
906 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
907 $rangmax = $this->
line_max($fk_parent_line);
908 $this->line->rang = $rangmax + 1;
911 $this->line->id = $rowid;
912 $this->line->label = $label;
913 $this->line->desc = $desc;
914 $this->line->qty = $qty;
915 $this->line->product_type = $type;
916 $this->line->vat_src_code = $vat_src_code;
917 $this->line->tva_tx = $txtva;
918 $this->line->localtax1_tx = $txlocaltax1;
919 $this->line->localtax2_tx = $txlocaltax2;
920 $this->line->localtax1_type = $localtaxes_type[0];
921 $this->line->localtax2_type = $localtaxes_type[2];
922 $this->line->remise_percent = $remise_percent;
923 $this->line->subprice = $pu_ht;
924 $this->line->info_bits = $info_bits;
926 $this->line->total_ht = $total_ht;
927 $this->line->total_tva = $total_tva;
928 $this->line->total_localtax1 = $total_localtax1;
929 $this->line->total_localtax2 = $total_localtax2;
930 $this->line->total_ttc = $total_ttc;
931 $this->line->special_code = $special_code;
932 $this->line->fk_parent_line = $fk_parent_line;
933 $this->line->skip_update_total = $skip_update_total;
934 $this->line->fk_unit = $fk_unit;
936 $this->line->fk_fournprice = $fk_fournprice;
937 $this->line->pa_ht = $pa_ht;
939 $this->line->date_start = $date_start;
940 $this->line->date_end = $date_end;
942 if (is_array($array_options) && count($array_options) > 0) {
944 foreach ($array_options as $key => $value) {
945 $this->line->array_options[$key] = $array_options[$key];
950 $this->line->multicurrency_subprice = $pu_ht_devise;
951 $this->line->multicurrency_total_ht = $multicurrency_total_ht;
952 $this->line->multicurrency_total_tva = $multicurrency_total_tva;
953 $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
955 $result = $this->line->update($notrigger);
958 if (!empty($fk_parent_line)) {
964 $this->fk_propal = $this->id;
965 $this->
rowid = $rowid;
970 $this->error = $this->line->error;
971 $this->errors = $this->line->errors;
972 $this->
db->rollback();
976 dol_syslog(get_class($this).
"::updateline Erreur -2 Propal en mode incompatible pour cette action");
992 if ($this->statut == self::STATUS_DRAFT) {
998 $line->fetch($lineid);
1000 if ($line->delete($user) > 0) {
1003 $this->
db->commit();
1006 $this->error = $line->error;
1007 $this->errors = $line->errors;
1008 $this->
db->rollback();
1012 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1026 public function create($user, $notrigger = 0)
1028 global $conf, $hookmanager, $mysoc;
1034 if (empty($this->date)) {
1037 $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
1038 if (empty($this->availability_id)) {
1039 $this->availability_id = 0;
1041 if (empty($this->demand_reason_id)) {
1042 $this->demand_reason_id = 0;
1046 if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1051 if (empty($this->fk_multicurrency)) {
1052 $this->multicurrency_code = $conf->currency;
1053 $this->fk_multicurrency = 0;
1054 $this->multicurrency_tx = 1;
1058 $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1065 $this->error =
"Failed to fetch company";
1066 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
1071 if (!empty($this->
ref)) {
1074 $this->error =
'ErrorRefAlreadyExists';
1075 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
1076 $this->
db->rollback();
1081 if (empty($this->date)) {
1082 $this->error =
"Date of proposal is required";
1083 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
1091 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"propal (";
1095 $sql .=
", remise_percent";
1096 $sql .=
", remise_absolue";
1097 $sql .=
", total_tva";
1098 $sql .=
", total_ttc";
1102 $sql .=
", fk_user_author";
1103 $sql .=
", note_private";
1104 $sql .=
", note_public";
1105 $sql .=
", model_pdf";
1106 $sql .=
", fin_validite";
1107 $sql .=
", fk_cond_reglement";
1108 $sql .=
", deposit_percent";
1109 $sql .=
", fk_mode_reglement";
1110 $sql .=
", fk_account";
1111 $sql .=
", ref_client";
1112 $sql .=
", ref_ext";
1113 $sql .=
", date_livraison";
1114 $sql .=
", fk_shipping_method";
1115 $sql .=
", fk_warehouse";
1116 $sql .=
", fk_availability";
1117 $sql .=
", fk_input_reason";
1118 $sql .=
", fk_projet";
1119 $sql .=
", fk_incoterms";
1120 $sql .=
", location_incoterms";
1122 $sql .=
", fk_multicurrency";
1123 $sql .=
", multicurrency_code";
1124 $sql .=
", multicurrency_tx";
1126 $sql .=
" VALUES (";
1127 $sql .= $this->socid;
1129 $sql .=
", ".((float) $this->remise);
1130 $sql .=
", ".($this->remise_percent ? ((
float) $this->remise_percent) :
'NULL');
1131 $sql .=
", ".($this->remise_absolue ? ((
float) $this->remise_absolue) :
'NULL');
1134 $sql .=
", '".$this->db->idate($this->date).
"'";
1135 $sql .=
", '".$this->db->idate($now).
"'";
1136 $sql .=
", '(PROV)'";
1137 $sql .=
", ".($user->id > 0 ? ((int) $user->id) :
"NULL");
1138 $sql .=
", '".$this->db->escape($this->note_private).
"'";
1139 $sql .=
", '".$this->db->escape($this->note_public).
"'";
1140 $sql .=
", '".$this->db->escape($this->model_pdf).
"'";
1141 $sql .=
", ".($this->fin_validite !=
'' ?
"'".$this->db->idate($this->fin_validite).
"'" :
"NULL");
1142 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
'NULL');
1143 $sql .=
", ".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
'NULL');
1144 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
'NULL');
1145 $sql .=
", ".($this->fk_account > 0 ? ((int) $this->fk_account) :
'NULL');
1146 $sql .=
", '".$this->db->escape($this->ref_client).
"'";
1147 $sql .=
", '".$this->db->escape($this->ref_ext).
"'";
1148 $sql .=
", ".(empty($delivery_date) ?
"NULL" :
"'".$this->db->idate($delivery_date).
"'");
1149 $sql .=
", ".($this->shipping_method_id > 0 ? $this->shipping_method_id :
'NULL');
1150 $sql .=
", ".($this->warehouse_id > 0 ? $this->warehouse_id :
'NULL');
1151 $sql .=
", ".$this->availability_id;
1152 $sql .=
", ".$this->demand_reason_id;
1153 $sql .=
", ".($this->fk_project ? $this->fk_project :
"null");
1154 $sql .=
", ".(int) $this->fk_incoterms;
1155 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
1156 $sql .=
", ".setEntity($this);
1157 $sql .=
", ".(int) $this->fk_multicurrency;
1158 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1159 $sql .=
", ".(double) $this->multicurrency_tx;
1162 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1165 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"propal");
1168 $this->
ref =
'(PROV'.$this->id.
')';
1169 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"propal SET ref='".$this->
db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
1171 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
1177 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1178 $this->linked_objects = $this->linkedObjectsIds;
1182 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1183 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1184 if (is_array($tmp_origin_id)) {
1185 foreach ($tmp_origin_id as $origin_id) {
1188 $this->error = $this->
db->lasterror();
1194 $origin_id = $tmp_origin_id;
1197 $this->error = $this->
db->lasterror();
1209 $fk_parent_line = 0;
1210 $num = count($this->lines);
1212 for ($i = 0; $i < $num; $i++) {
1213 if (!is_object($this->lines[$i])) {
1215 $line = (object) $this->lines[$i];
1217 $line = $this->lines[$i];
1220 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1221 $fk_parent_line = 0;
1224 $vatrate = $line->tva_tx;
1225 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
1226 $vatrate .=
' ('.$line->vat_src_code.
')';
1229 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1230 $originid = $line->origin_id;
1231 $origintype = $line->origin;
1233 $originid = $line->id;
1234 $origintype = $this->element;
1242 $line->localtax1_tx,
1243 $line->localtax2_tx,
1245 $line->remise_percent,
1249 $line->product_type,
1251 $line->special_code,
1253 $line->fk_fournprice,
1258 $line->array_options,
1269 $this->error = $this->
db->error;
1274 if ($result > 0 && $line->product_type == 9) {
1275 $fk_parent_line = $result;
1305 if (!$error && !$notrigger) {
1314 $this->error = $this->
db->lasterror();
1319 $this->error = $this->
db->lasterror();
1324 $this->
db->commit();
1325 dol_syslog(get_class($this).
"::create done id=".$this->
id);
1328 $this->
db->rollback();
1332 $this->error = $this->
db->lasterror();
1333 $this->
db->rollback();
1349 global $conf, $hookmanager, $mysoc;
1358 $object =
new self($this->db);
1363 $object->fetch($this->
id);
1368 if (!empty($socid) && $socid != $object->socid) {
1369 if ($objsoc->fetch($socid) > 0) {
1370 $object->socid = $objsoc->id;
1371 $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1372 $object->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent :
null);
1373 $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1374 $object->fk_delivery_address =
'';
1386 $object->fk_project =
'';
1390 $object->ref_client =
'';
1394 $objsoc->fetch($object->socid);
1398 if ($update_prices ===
true) {
1399 if ($objsoc->id > 0 && !empty($object->lines)) {
1400 if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1402 require_once DOL_DOCUMENT_ROOT .
'/product/class/productcustomerprice.class.php';
1405 foreach ($object->lines as $line) {
1406 if ($line->fk_product > 0) {
1408 $res = $prod->fetch($line->fk_product);
1410 $pu_ht = $prod->price;
1412 $remise_percent = $objsoc->remise_percent;
1414 if (!empty($conf->global->PRODUIT_MULTIPRICES) && $objsoc->price_level > 0) {
1415 $pu_ht = $prod->multiprices[$objsoc->price_level];
1416 if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
1417 if (isset($prod->multiprices_tva_tx[$objsoc->price_level])) {
1418 $tva_tx = $prod->multiprices_tva_tx[$objsoc->price_level];
1421 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1423 $filter = array(
't.fk_product' => $prod->id,
't.fk_soc' => $objsoc->id);
1424 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
1427 if (count($prodcustprice->lines) > 0) {
1428 $pu_ht =
price($prodcustprice->lines[0]->price);
1429 $tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.
' ('.$prodcustprice->lines[0]->default_vat_code.
' )' : $prodcustprice->lines[0]->tva_tx);
1430 if ($prodcustprice->lines[0]->default_vat_code && !preg_match(
'/\(.*\)/', $tva_tx)) {
1431 $tva_tx .=
' ('.$prodcustprice->lines[0]->default_vat_code.
')';
1437 $line->subprice = $pu_ht;
1438 $line->tva_tx = $tva_tx;
1439 $line->remise_percent = $remise_percent;
1448 $object->entity = (!empty($forceentity) ? $forceentity : $object->entity);
1452 $object->user_author = $user->id;
1453 $object->user_valid = 0;
1454 $object->date = $now;
1455 $object->datep = $now;
1456 $object->fin_validite = $object->date + ($object->duree_validite * 24 * 3600);
1457 if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
1458 $object->ref_client =
'';
1461 $object->note_private =
'';
1462 $object->note_public =
'';
1465 $object->context[
'createfromclone'] =
'createfromclone';
1466 $result = $object->create($user);
1468 $this->error = $object->error;
1469 $this->errors = array_merge($this->errors, $object->errors);
1475 if ($object->copy_linked_contact($this,
'internal') < 0) {
1482 if ($this->socid == $object->socid) {
1483 if ($object->copy_linked_contact($this,
'external') < 0) {
1491 if (is_object($hookmanager)) {
1492 $parameters = array(
'objFrom'=>$this,
'clonedObj'=>$object);
1494 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $object, $action);
1496 $this->errors += $hookmanager->errors;
1497 $this->error = $hookmanager->error;
1503 unset($object->context[
'createfromclone']);
1507 $this->
db->commit();
1510 $this->
db->rollback();
1524 public function fetch($rowid, $ref =
'', $ref_ext =
'', $forceentity = 0)
1526 $sql =
"SELECT p.rowid, p.ref, p.entity, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
1527 $sql .=
", p.total_ttc, p.total_tva, p.localtax1, p.localtax2, p.total_ht";
1528 $sql .=
", p.datec";
1529 $sql .=
", p.date_signature as dates";
1530 $sql .=
", p.date_valid as datev";
1531 $sql .=
", p.datep as dp";
1532 $sql .=
", p.fin_validite as dfv";
1533 $sql .=
", p.date_livraison as delivery_date";
1534 $sql .=
", p.model_pdf, p.last_main_doc, p.ref_client, ref_ext, p.extraparams";
1535 $sql .=
", p.note_private, p.note_public";
1536 $sql .=
", p.fk_projet as fk_project, p.fk_statut";
1537 $sql .=
", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
1538 $sql .=
", p.fk_delivery_address";
1539 $sql .=
", p.fk_availability";
1540 $sql .=
", p.fk_input_reason";
1541 $sql .=
", p.fk_cond_reglement";
1542 $sql .=
", p.fk_mode_reglement";
1543 $sql .=
', p.fk_account';
1544 $sql .=
", p.fk_shipping_method";
1545 $sql .=
", p.fk_warehouse";
1546 $sql .=
", p.fk_incoterms, p.location_incoterms";
1547 $sql .=
", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc";
1548 $sql .=
", p.tms as date_modification";
1549 $sql .=
", i.libelle as label_incoterms";
1550 $sql .=
", c.label as statut_label";
1551 $sql .=
", ca.code as availability_code, ca.label as availability";
1552 $sql .=
", dr.code as demand_reason_code, dr.label as demand_reason";
1553 $sql .=
", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc, p.deposit_percent";
1554 $sql .=
", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
1555 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal as p";
1556 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_propalst as c ON p.fk_statut = c.id';
1557 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as cp ON p.fk_mode_reglement = cp.id AND cp.entity IN ('.
getEntity(
'c_paiement').
')';
1558 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid AND cr.entity IN ('.
getEntity(
'c_payment_term').
')';
1559 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_availability as ca ON p.fk_availability = ca.rowid';
1560 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_input_reason as dr ON p.fk_input_reason = dr.rowid';
1561 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON p.fk_incoterms = i.rowid';
1564 if (!empty($forceentity)) {
1565 $sql .=
" WHERE p.entity = ".(int) $forceentity;
1567 $sql .=
" WHERE p.entity IN (".getEntity(
'propal').
")";
1569 $sql .=
" AND p.ref='".$this->db->escape($ref).
"'";
1572 $sql .=
" WHERE p.rowid = ".((int) $rowid);
1575 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1579 $obj = $this->
db->fetch_object(
$resql);
1581 $this->
id = $obj->rowid;
1582 $this->entity = $obj->entity;
1584 $this->
ref = $obj->ref;
1585 $this->ref_client = $obj->ref_client;
1586 $this->ref_ext = $obj->ref_ext;
1587 $this->remise = $obj->remise;
1588 $this->remise_percent = $obj->remise_percent;
1589 $this->remise_absolue = $obj->remise_absolue;
1590 $this->total = $obj->total_ttc;
1591 $this->total_ttc = $obj->total_ttc;
1592 $this->total_ht = $obj->total_ht;
1593 $this->total_tva = $obj->total_tva;
1594 $this->total_localtax1 = $obj->localtax1;
1595 $this->total_localtax2 = $obj->localtax2;
1597 $this->socid = $obj->fk_soc;
1598 $this->thirdparty =
null;
1600 $this->fk_project = $obj->fk_project;
1601 $this->project =
null;
1603 $this->model_pdf = $obj->model_pdf;
1604 $this->modelpdf = $obj->model_pdf;
1605 $this->last_main_doc = $obj->last_main_doc;
1606 $this->note = $obj->note_private;
1607 $this->note_private = $obj->note_private;
1608 $this->note_public = $obj->note_public;
1610 $this->status = (int) $obj->fk_statut;
1611 $this->statut = $this->status;
1612 $this->statut_libelle = $obj->statut_label;
1614 $this->datec = $this->db->jdate($obj->datec);
1615 $this->datev = $this->
db->jdate($obj->datev);
1616 $this->date_creation = $this->
db->jdate($obj->datec);
1617 $this->date_validation = $this->
db->jdate($obj->datev);
1618 $this->date_modification = $this->
db->jdate($obj->date_modification);
1619 $this->date_signature = $this->
db->jdate($obj->dates);
1620 $this->date = $this->
db->jdate($obj->dp);
1621 $this->datep = $this->
db->jdate($obj->dp);
1622 $this->fin_validite = $this->
db->jdate($obj->dfv);
1623 $this->date_livraison = $this->
db->jdate($obj->delivery_date);
1624 $this->delivery_date = $this->
db->jdate($obj->delivery_date);
1625 $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method :
null;
1626 $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse :
null;
1627 $this->availability_id = $obj->fk_availability;
1628 $this->availability_code = $obj->availability_code;
1630 $this->demand_reason_id = $obj->fk_input_reason;
1631 $this->demand_reason_code = $obj->demand_reason_code;
1633 $this->fk_address = $obj->fk_delivery_address;
1635 $this->mode_reglement_id = $obj->fk_mode_reglement;
1636 $this->mode_reglement_code = $obj->mode_reglement_code;
1637 $this->mode_reglement = $obj->mode_reglement;
1638 $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account :
null;
1639 $this->cond_reglement_id = $obj->fk_cond_reglement;
1640 $this->cond_reglement_code = $obj->cond_reglement_code;
1641 $this->cond_reglement = $obj->cond_reglement;
1642 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1643 $this->deposit_percent = $obj->deposit_percent;
1645 $this->extraparams = (array) json_decode($obj->extraparams,
true);
1647 $this->user_author_id = $obj->fk_user_author;
1648 $this->user_valid_id = $obj->fk_user_valid;
1649 $this->user_close_id = $obj->fk_user_cloture;
1652 $this->fk_incoterms = $obj->fk_incoterms;
1653 $this->location_incoterms = $obj->location_incoterms;
1654 $this->label_incoterms = $obj->label_incoterms;
1657 $this->fk_multicurrency = $obj->fk_multicurrency;
1658 $this->multicurrency_code = $obj->multicurrency_code;
1659 $this->multicurrency_tx = $obj->multicurrency_tx;
1660 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1661 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1662 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1664 if ($obj->fk_statut == self::STATUS_DRAFT) {
1665 $this->brouillon = 1;
1674 $this->lines = array();
1685 $this->error =
"Record Not Found";
1688 $this->error = $this->
db->lasterror();
1707 if (isset($this->
ref)) {
1708 $this->
ref = trim($this->
ref);
1710 if (isset($this->ref_client)) {
1711 $this->ref_client = trim($this->ref_client);
1713 if (isset($this->note) || isset($this->note_private)) {
1714 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
1716 if (isset($this->note_public)) {
1717 $this->note_public = trim($this->note_public);
1719 if (isset($this->model_pdf)) {
1720 $this->model_pdf = trim($this->model_pdf);
1722 if (isset($this->import_key)) {
1723 $this->import_key = trim($this->import_key);
1725 if (!empty($this->duree_validite) && is_numeric($this->duree_validite)) {
1726 $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
1733 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal SET";
1734 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1735 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
1736 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1737 $sql .=
" fk_soc=".(isset($this->socid) ? $this->socid :
"null").
",";
1738 $sql .=
" datep=".(strval($this->date) !=
'' ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
1739 if (!empty($this->fin_validite)) {
1740 $sql .=
" fin_validite=".(strval($this->fin_validite) !=
'' ?
"'".$this->db->idate($this->fin_validite).
"'" :
'null').
",";
1742 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
1743 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
1744 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
1745 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
1746 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
1747 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
1748 $sql .=
" fk_statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1749 $sql .=
" fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id :
"null").
",";
1750 $sql .=
" fk_user_valid=".(isset($this->user_valid) ? $this->user_valid :
"null").
",";
1751 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->fk_project :
"null").
",";
1752 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id :
"null").
",";
1753 $sql .=
" deposit_percent=".(!empty($this->deposit_percent) ?
"'".$this->db->escape($this->deposit_percent).
"'" :
"null").
",";
1754 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id :
"null").
",";
1755 $sql .=
" fk_input_reason=".(isset($this->demand_reason_id) ? $this->demand_reason_id :
"null").
",";
1756 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1757 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1758 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
1759 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
"";
1760 $sql .=
" WHERE rowid=".((int) $this->
id);
1764 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1768 $this->errors[] =
"Error ".$this->db->lasterror();
1778 if (!$error && !$notrigger) {
1789 foreach ($this->errors as $errmsg) {
1790 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1791 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1793 $this->
db->rollback();
1796 $this->
db->commit();
1812 public function fetch_lines($only_product = 0, $loadalsotranslation = 0, $filters =
'')
1815 global $langs, $conf;
1817 $this->lines = array();
1819 $sql =
'SELECT d.rowid, d.fk_propal, d.fk_parent_line, d.label as custom_label, d.description, d.price, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,';
1820 $sql .=
' d.info_bits, d.total_ht, d.total_tva, d.total_localtax1, d.total_localtax2, d.total_ttc, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht, d.special_code, d.rang, d.product_type,';
1821 $sql .=
' d.fk_unit,';
1822 $sql .=
' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label, p.tobatch as product_tobatch, p.barcode as product_barcode,';
1823 $sql .=
' p.weight, p.weight_units, p.volume, p.volume_units,';
1824 $sql .=
' d.date_start, d.date_end,';
1825 $sql .=
' d.fk_multicurrency, d.multicurrency_code, d.multicurrency_subprice, d.multicurrency_total_ht, d.multicurrency_total_tva, d.multicurrency_total_ttc';
1826 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as d';
1827 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON (d.fk_product = p.rowid)';
1828 $sql .=
' WHERE d.fk_propal = '.((int) $this->
id);
1829 if ($only_product) {
1830 $sql .=
' AND p.fk_product_type = 0';
1835 $sql .=
' ORDER by d.rang';
1837 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
1838 $result = $this->
db->query($sql);
1840 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
1842 $num = $this->
db->num_rows($result);
1846 $objp = $this->
db->fetch_object($result);
1850 $line->rowid = $objp->rowid;
1851 $line->id = $objp->rowid;
1852 $line->fk_propal = $objp->fk_propal;
1853 $line->fk_parent_line = $objp->fk_parent_line;
1854 $line->product_type = $objp->product_type;
1855 $line->label = $objp->custom_label;
1856 $line->desc = $objp->description;
1857 $line->description = $objp->description;
1858 $line->qty = $objp->qty;
1859 $line->vat_src_code = $objp->vat_src_code;
1860 $line->tva_tx = $objp->tva_tx;
1861 $line->localtax1_tx = $objp->localtax1_tx;
1862 $line->localtax2_tx = $objp->localtax2_tx;
1863 $line->localtax1_type = $objp->localtax1_type;
1864 $line->localtax2_type = $objp->localtax2_type;
1865 $line->subprice = $objp->subprice;
1866 $line->fk_remise_except = $objp->fk_remise_except;
1867 $line->remise_percent = $objp->remise_percent;
1868 $line->price = $objp->price;
1870 $line->info_bits = $objp->info_bits;
1871 $line->total_ht = $objp->total_ht;
1872 $line->total_tva = $objp->total_tva;
1873 $line->total_localtax1 = $objp->total_localtax1;
1874 $line->total_localtax2 = $objp->total_localtax2;
1875 $line->total_ttc = $objp->total_ttc;
1876 $line->fk_fournprice = $objp->fk_fournprice;
1877 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
1878 $line->pa_ht = $marginInfos[0];
1879 $line->marge_tx = $marginInfos[1];
1880 $line->marque_tx = $marginInfos[2];
1881 $line->special_code = $objp->special_code;
1882 $line->rang = $objp->rang;
1884 $line->fk_product = $objp->fk_product;
1886 $line->ref = $objp->product_ref;
1887 $line->libelle = $objp->product_label;
1889 $line->product_ref = $objp->product_ref;
1890 $line->product_label = $objp->product_label;
1891 $line->product_desc = $objp->product_desc;
1892 $line->product_tobatch = $objp->product_tobatch;
1893 $line->product_barcode = $objp->product_barcode;
1895 $line->fk_product_type = $objp->fk_product_type;
1896 $line->fk_unit = $objp->fk_unit;
1897 $line->weight = $objp->weight;
1898 $line->weight_units = $objp->weight_units;
1899 $line->volume = $objp->volume;
1900 $line->volume_units = $objp->volume_units;
1902 $line->date_start = $this->
db->jdate($objp->date_start);
1903 $line->date_end = $this->
db->jdate($objp->date_end);
1906 $line->fk_multicurrency = $objp->fk_multicurrency;
1907 $line->multicurrency_code = $objp->multicurrency_code;
1908 $line->multicurrency_subprice = $objp->multicurrency_subprice;
1909 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
1910 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
1911 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
1913 $line->fetch_optionals();
1916 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
1918 $tmpproduct->fetch($objp->fk_product);
1919 $tmpproduct->getMultiLangs();
1921 $line->multilangs = $tmpproduct->multilangs;
1924 $this->lines[$i] = $line;
1929 $this->
db->free($result);
1933 $this->error = $this->
db->lasterror();
1945 public function valid($user, $notrigger = 0)
1949 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1954 if ($this->statut == self::STATUS_VALIDATED) {
1955 dol_syslog(get_class($this).
"::valid action abandonned: already validated", LOG_WARNING);
1959 if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->creer))
1960 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->validate)))) {
1961 $this->error =
'ErrorPermissionDenied';
1962 dol_syslog(get_class($this).
"::valid ".$this->error, LOG_ERR);
1972 $soc->fetch($this->socid);
1975 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
1982 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal";
1983 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
1984 $sql .=
" fk_statut = ".self::STATUS_VALIDATED.
", date_valid='".$this->
db->idate($now).
"', fk_user_valid=".((int) $user->id);
1987 dol_syslog(get_class($this).
"::valid", LOG_DEBUG);
1995 if (!$error && !$notrigger) {
1997 $result = $this->
call_trigger(
'PROPAL_VALIDATE', $user);
2005 $this->oldref = $this->ref;
2008 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
2010 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->
db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'propale/".$this->
db->escape($this->newref).
"'";
2011 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'propale/".$this->
db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
2015 $this->error = $this->
db->lasterror();
2021 $dirsource = $conf->propal->multidir_output[$this->entity].
'/'.$oldref;
2022 $dirdest = $conf->propal->multidir_output[$this->entity].
'/'.$newref;
2023 if (!$error && file_exists($dirsource)) {
2024 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
2025 if (@rename($dirsource, $dirdest)) {
2028 $listoffiles =
dol_dir_list($dirdest,
'files', 1,
'^'.preg_quote($oldref,
'/'));
2029 foreach ($listoffiles as $fileentry) {
2030 $dirsource = $fileentry[
'name'];
2031 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
2032 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
2033 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
2034 @rename($dirsource, $dirdest);
2041 $this->brouillon = 0;
2043 $this->user_valid_id = $user->id;
2044 $this->datev = $now;
2046 $this->
db->commit();
2049 $this->
db->rollback();
2068 $this->error =
'ErrorBadParameter';
2069 dol_syslog(get_class($this).
"::set_date ".$this->error, LOG_ERR);
2073 if (!empty($user->rights->propal->creer)) {
2078 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal SET datep = '".$this->
db->idate($date).
"'";
2084 $this->errors[] = $this->
db->error();
2089 $this->oldcopy = clone $this;
2090 $this->date = $date;
2091 $this->datep = $date;
2094 if (!$notrigger && empty($error)) {
2104 $this->
db->commit();
2107 foreach ($this->errors as $errmsg) {
2108 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2109 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2111 $this->
db->rollback();
2129 if (!empty($user->rights->propal->creer)) {
2134 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal SET fin_validite = ".($date_end_validity !=
'' ?
"'".$this->db->idate($date_end_validity).
"'" :
'null');
2135 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND fk_statut = ".((int) self::STATUS_DRAFT);
2140 $this->errors[] = $this->
db->error();
2146 $this->oldcopy = clone $this;
2147 $this->fin_validite = $date_end_validity;
2150 if (!$notrigger && empty($error)) {
2160 $this->
db->commit();
2163 foreach ($this->errors as $errmsg) {
2164 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2165 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2167 $this->
db->rollback();
2199 if (!empty($user->rights->propal->creer)) {
2204 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal ";
2205 $sql .=
" SET date_livraison = ".($delivery_date !=
'' ?
"'".$this->db->idate($delivery_date).
"'" :
'null');
2206 $sql .=
" WHERE rowid = ".((int) $this->
id);
2211 $this->errors[] = $this->
db->error();
2216 $this->oldcopy = clone $this;
2217 $this->date_livraison = $delivery_date;
2218 $this->delivery_date = $delivery_date;
2221 if (!$notrigger && empty($error)) {
2231 $this->
db->commit();
2234 foreach ($this->errors as $errmsg) {
2235 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2236 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2238 $this->
db->rollback();
2256 if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) {
2261 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal";
2262 $sql .=
" SET fk_availability = ".((int) $id);
2263 $sql .=
" WHERE rowid = ".((int) $this->
id);
2265 dol_syslog(__METHOD__.
' availability('.$id.
')', LOG_DEBUG);
2268 $this->errors[] = $this->
db->error();
2273 $this->oldcopy = clone $this;
2274 $this->fk_availability = $id;
2275 $this->availability_id = $id;
2278 if (!$notrigger && empty($error)) {
2288 $this->
db->commit();
2291 foreach ($this->errors as $errmsg) {
2292 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2293 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2295 $this->
db->rollback();
2299 $error_str =
'Propal status do not meet requirement '.$this->statut;
2301 $this->error = $error_str;
2302 $this->errors[] = $this->error;
2319 if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) {
2324 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal ";
2325 $sql .=
" SET fk_input_reason = ".((int) $id);
2326 $sql .=
" WHERE rowid = ".((int) $this->
id);
2331 $this->errors[] = $this->
db->error();
2337 $this->oldcopy = clone $this;
2338 $this->fk_input_reason = $id;
2339 $this->demand_reason_id = $id;
2343 if (!$notrigger && empty($error)) {
2353 $this->
db->commit();
2356 foreach ($this->errors as $errmsg) {
2357 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2358 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2360 $this->
db->rollback();
2364 $error_str =
'Propal status do not meet requirement '.$this->statut;
2366 $this->error = $error_str;
2367 $this->errors[] = $this->error;
2384 if (!empty($user->rights->propal->creer)) {
2389 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal SET ref_client = ".(empty($ref_client) ?
'NULL' :
"'".$this->db->escape($ref_client).
"'");
2390 $sql .=
" WHERE rowid = ".((int) $this->
id);
2392 dol_syslog(__METHOD__.
' $this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2395 $this->errors[] = $this->
db->error();
2400 $this->oldcopy = clone $this;
2401 $this->ref_client = $ref_client;
2404 if (!$notrigger && empty($error)) {
2414 $this->
db->commit();
2417 foreach ($this->errors as $errmsg) {
2418 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2419 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2421 $this->
db->rollback();
2443 if (!empty($user->rights->propal->creer)) {
2450 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal SET remise_percent = ".((
float)
$remise);
2456 $this->errors[] = $this->
db->error();
2461 $this->oldcopy = clone $this;
2462 $this->remise_percent =
$remise;
2466 if (!$notrigger && empty($error)) {
2476 $this->
db->commit();
2479 foreach ($this->errors as $errmsg) {
2480 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2481 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2483 $this->
db->rollback();
2507 if (!empty($user->rights->propal->creer)) {
2512 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal";
2513 $sql .=
" SET remise_absolue = ".((float)
$remise);
2519 $this->errors[] = $this->
db->error();
2524 $this->oldcopy = clone $this;
2528 if (!$notrigger && empty($error)) {
2538 $this->
db->commit();
2541 foreach ($this->errors as $errmsg) {
2542 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2543 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2545 $this->
db->rollback();
2566 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal";
2567 $sql .=
" SET fk_statut = ".((int) $status).
",";
2568 if (!empty(
$note)) {
2569 $sql .=
" note_private = '".$this->db->escape(
$note).
"',";
2571 $sql .=
" date_cloture=NULL, fk_user_cloture=NULL";
2572 $sql .=
" WHERE rowid = ".((int) $this->
id);
2576 dol_syslog(get_class($this).
"::reopen", LOG_DEBUG);
2580 $this->errors[] =
"Error ".$this->db->lasterror();
2595 if (!empty($this->errors)) {
2596 foreach ($this->errors as $errmsg) {
2597 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
2598 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2601 $this->
db->rollback();
2604 $this->statut = $status;
2605 $this->status = $status;
2607 $this->
db->commit();
2623 global $langs,$conf;
2632 if (empty($conf->global->PROPALE_KEEP_OLD_SIGNATURE_INFO)) {
2633 $date_signature = $now;
2634 $fk_user_signature = $user->id;
2636 $this->
info($this->
id);
2637 if (!isset($this->date_signature) || $this->date_signature ==
'') {
2638 $date_signature = $now;
2639 $fk_user_signature = $user->id;
2641 $date_signature = $this->date_signature;
2642 $fk_user_signature = $this->user_signature->id;
2646 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal";
2647 $sql .=
" SET fk_statut = ".((int) $status).
", note_private = '".$this->
db->escape($newprivatenote).
"', date_signature='".$this->
db->idate($date_signature).
"', fk_user_signature=".$fk_user_signature;
2648 $sql .=
" WHERE rowid = ".((int) $this->
id);
2653 $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_CLOSED) ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf;
2654 $trigger_name =
'PROPAL_CLOSE_REFUSED';
2656 if ($status == self::STATUS_SIGNED) {
2657 $trigger_name =
'PROPAL_CLOSE_SIGNED';
2658 $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_TOBILL) ? $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL : $this->model_pdf;
2662 $soc->id = $this->socid;
2663 $result = $soc->set_as_client();
2666 $this->error=$this->
db->lasterror();
2667 $this->
db->rollback();
2672 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
2674 $outputlangs = $langs;
2676 $outputlangs =
new Translate(
"", $conf);
2677 $newlang = (
GETPOST(
'lang_id',
'aZ09') ?
GETPOST(
'lang_id',
'aZ09') : $this->thirdparty->default_lang);
2678 $outputlangs->setDefaultLang($newlang);
2682 $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
2683 $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
2684 $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
2687 $this->
generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
2691 $this->oldcopy= clone $this;
2692 $this->statut = $status;
2693 $this->status = $status;
2694 $this->date_signature = $date_signature;
2695 $this->note_private = $newprivatenote;
2698 if (!$notrigger && empty($error)) {
2708 $this->
db->commit();
2711 $this->statut = $this->oldcopy->statut;
2712 $this->status = $this->oldcopy->statut;
2713 $this->date_signature = $this->oldcopy->date_signature;
2714 $this->note_private = $this->oldcopy->note_private;
2716 $this->
db->rollback();
2720 $this->error = $this->
db->lasterror();
2721 $this->
db->rollback();
2736 global $conf, $langs;
2743 $triggerName =
'PROPAL_CLASSIFY_BILLED';
2749 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'propal SET fk_statut = '.self::STATUS_BILLED.
", ";
2750 $sql .=
" note_private = '".$this->db->escape($newprivatenote).
"', date_cloture='".$this->
db->idate($now).
"', fk_user_cloture=".((int) $user->id);
2751 $sql .=
' WHERE rowid = '.((int) $this->
id).
' AND fk_statut = '.((int) self::STATUS_SIGNED);
2756 $this->errors[] = $this->
db->error();
2759 $num = $this->
db->affected_rows(
$resql);
2763 $modelpdf = $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf;
2765 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
2767 $outputlangs = $langs;
2769 $outputlangs =
new Translate(
"", $conf);
2770 $newlang = (
GETPOST(
'lang_id',
'aZ09') ?
GETPOST(
'lang_id',
'aZ09') : $this->thirdparty->default_lang);
2771 $outputlangs->setDefaultLang($newlang);
2775 $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
2776 $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
2777 $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
2780 $this->
generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
2783 $this->oldcopy = clone $this;
2785 $this->date_cloture = $now;
2786 $this->note_private = $newprivatenote;
2789 if (!$notrigger && empty($error)) {
2799 $this->
db->commit();
2802 foreach ($this->errors as $errmsg) {
2803 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2804 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2806 $this->
db->rollback();
2825 if ($this->statut <= self::STATUS_DRAFT) {
2829 dol_syslog(get_class($this).
"::setDraft", LOG_DEBUG);
2833 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propal";
2834 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
2835 $sql .=
", online_sign_ip = NULL , online_sign_name = NULL";
2836 $sql .=
" WHERE rowid = ".((int) $this->
id);
2840 $this->errors[] = $this->
db->error();
2845 $this->oldcopy = clone $this;
2848 if (!$notrigger && empty($error)) {
2859 $this->brouillon = 1;
2861 $this->
db->commit();
2864 foreach ($this->errors as $errmsg) {
2865 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2866 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2868 $this->
db->rollback();
2888 public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield =
'p.datep', $sortorder =
'DESC')
2895 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
2896 $sql .=
" p.rowid as propalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
2897 $sql .=
" p.datep as dp, p.fin_validite as datelimite";
2898 if (empty($user->rights->societe->client->voir) && !$socid) {
2899 $sql .=
", sc.fk_soc, sc.fk_user";
2901 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"propal as p, ".MAIN_DB_PREFIX.
"c_propalst as c";
2902 if (empty($user->rights->societe->client->voir) && !$socid) {
2903 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2905 $sql .=
" WHERE p.entity IN (".getEntity(
'propal').
")";
2906 $sql .=
" AND p.fk_soc = s.rowid";
2907 $sql .=
" AND p.fk_statut = c.id";
2908 if (empty($user->rights->societe->client->voir) && !$socid) {
2909 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2912 $sql .=
" AND s.rowid = ".((int) $socid);
2915 $sql .=
" AND p.fk_statut = ".self::STATUS_DRAFT;
2917 if ($notcurrentuser > 0) {
2918 $sql .=
" AND p.fk_user_author <> ".((int) $user->id);
2920 $sql .= $this->
db->order($sortfield, $sortorder);
2921 $sql .= $this->
db->plimit($limit, $offset);
2923 $result = $this->
db->query($sql);
2925 $num = $this->
db->num_rows($result);
2929 $obj = $this->
db->fetch_object($result);
2931 if ($shortlist == 1) {
2932 $ga[$obj->propalid] = $obj->ref;
2933 } elseif ($shortlist == 2) {
2934 $ga[$obj->propalid] = $obj->ref.
' ('.$obj->name.
')';
2936 $ga[$i][
'id'] = $obj->propalid;
2937 $ga[$i][
'ref'] = $obj->ref;
2938 $ga[$i][
'name'] = $obj->name;
2972 $linkedInvoices = array();
2975 foreach ($this->linkedObjectsIds as $objecttype => $objectid) {
2978 foreach ($objectid as $key => $object) {
2980 if ($objecttype ==
'facture') {
2981 $linkedInvoices[] = $object;
2985 foreach ($this->linkedObjectsIds as $subobjecttype => $subobjectid) {
2986 foreach ($subobjectid as $subkey => $subobject) {
2987 if ($subobjecttype ==
'facture') {
2988 $linkedInvoices[] = $subobject;
2996 if (count($linkedInvoices) > 0) {
2997 $sql =
"SELECT rowid as facid, ref, total_ht as total, datef as df, fk_user_author, fk_statut, paye";
2998 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
2999 $sql .=
" WHERE rowid IN (".$this->db->sanitize(implode(
',', $linkedInvoices)).
")";
3001 dol_syslog(get_class($this).
"::InvoiceArrayList", LOG_DEBUG);
3005 $tab_sqlobj = array();
3006 $nump = $this->
db->num_rows(
$resql);
3007 for ($i = 0; $i < $nump; $i++) {
3008 $sqlobj = $this->
db->fetch_object(
$resql);
3009 $tab_sqlobj[] = $sqlobj;
3013 $nump = count($tab_sqlobj);
3017 while ($i < $nump) {
3018 $obj = array_shift($tab_sqlobj);
3041 public function delete($user, $notrigger = 0)
3044 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3060 if (!$error && !empty($this->table_element_line)) {
3061 $tabletodelete = $this->table_element_line;
3062 $sqlef =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
"_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id).
")";
3063 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete.
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3064 if (!$this->
db->query($sqlef) || !$this->
db->query($sql)) {
3066 $this->error = $this->
db->lasterror();
3067 $this->errors[] = $this->error;
3068 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3093 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3099 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element.
" WHERE rowid = ".((int) $this->
id);
3100 $res = $this->
db->query($sql);
3103 $this->error = $this->
db->lasterror();
3104 $this->errors[] = $this->error;
3105 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
3120 if ($conf->propal->multidir_output[$this->entity] && !empty($this->
ref)) {
3121 $dir = $conf->propal->multidir_output[$this->entity].
"/".$ref;
3122 $file = $dir.
"/".$ref.
".pdf";
3123 if (file_exists($file)) {
3127 $this->error =
'ErrorFailToDeleteFile';
3128 $this->errors[] = $this->error;
3129 $this->
db->rollback();
3133 if (file_exists($dir)) {
3136 $this->error =
'ErrorFailToDeleteDir';
3137 $this->errors[] = $this->error;
3138 $this->
db->rollback();
3146 dol_syslog(get_class($this).
"::delete ".$this->
id.
" by ".$user->id, LOG_DEBUG);
3147 $this->
db->commit();
3150 $this->
db->rollback();
3167 if ($this->statut >= self::STATUS_DRAFT) {
3172 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'propal';
3173 $sql .=
' SET fk_availability = '.((int) $availability_id);
3174 $sql .=
' WHERE rowid='.((int) $this->
id);
3176 dol_syslog(__METHOD__.
' availability('.$availability_id.
')', LOG_DEBUG);
3179 $this->errors[] = $this->
db->error();
3184 $this->oldcopy = clone $this;
3185 $this->availability_id = $availability_id;
3188 if (!$notrigger && empty($error)) {
3198 $this->
db->commit();
3201 foreach ($this->errors as $errmsg) {
3202 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
3203 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3205 $this->
db->rollback();
3209 $error_str =
'Propal status do not meet requirement '.$this->statut;
3211 $this->error = $error_str;
3212 $this->errors[] = $this->error;
3231 if ($this->statut >= self::STATUS_DRAFT) {
3236 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'propal';
3237 $sql .=
' SET fk_input_reason = '.((int) $demand_reason_id);
3238 $sql .=
' WHERE rowid='.((int) $this->
id);
3240 dol_syslog(__METHOD__.
' demand_reason('.$demand_reason_id.
')', LOG_DEBUG);
3243 $this->errors[] = $this->
db->error();
3248 $this->oldcopy = clone $this;
3249 $this->demand_reason_id = $demand_reason_id;
3252 if (!$notrigger && empty($error)) {
3262 $this->
db->commit();
3265 foreach ($this->errors as $errmsg) {
3266 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
3267 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
3269 $this->
db->rollback();
3273 $error_str =
'Propal status do not meet requirement '.$this->statut;
3275 $this->error = $error_str;
3276 $this->errors[] = $this->error;
3290 $sql =
"SELECT c.rowid, ";
3291 $sql .=
" c.datec, c.date_valid as datev, c.date_signature, c.date_cloture,";
3292 $sql .=
" c.fk_user_author, c.fk_user_valid, c.fk_user_signature, c.fk_user_cloture";
3293 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal as c";
3294 $sql .=
" WHERE c.rowid = ".((int) $id);
3296 $result = $this->
db->query($sql);
3299 if ($this->
db->num_rows($result)) {
3300 $obj = $this->
db->fetch_object($result);
3302 $this->
id = $obj->rowid;
3304 $this->date_creation = $this->
db->jdate($obj->datec);
3305 $this->date_validation = $this->
db->jdate($obj->datev);
3306 $this->date_signature = $this->
db->jdate($obj->date_signature);
3307 $this->date_cloture = $this->
db->jdate($obj->date_cloture);
3309 $cuser =
new User($this->
db);
3310 $cuser->fetch($obj->fk_user_author);
3311 $this->user_creation = $cuser;
3313 if ($obj->fk_user_valid) {
3314 $vuser =
new User($this->
db);
3315 $vuser->fetch($obj->fk_user_valid);
3316 $this->user_validation = $vuser;
3319 if ($obj->fk_user_signature) {
3320 $user_signature =
new User($this->
db);
3321 $user_signature->fetch($obj->fk_user_signature);
3322 $this->user_signature = $user_signature;
3325 if ($obj->fk_user_cloture) {
3326 $cluser =
new User($this->
db);
3327 $cluser->fetch($obj->fk_user_cloture);
3328 $this->user_cloture = $cluser;
3331 $this->
db->free($result);
3346 return $this->
LibStatut($this->statut, $mode);
3360 global $conf, $hookmanager;
3363 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
3365 $langs->load(
"propal");
3366 $this->labelStatus[0] = $langs->transnoentitiesnoconv(
"PropalStatusDraft");
3367 $this->labelStatus[1] = $langs->transnoentitiesnoconv(
"PropalStatusValidated");
3368 $this->labelStatus[2] = $langs->transnoentitiesnoconv(
"PropalStatusSigned");
3369 $this->labelStatus[3] = $langs->transnoentitiesnoconv(
"PropalStatusNotSigned");
3370 $this->labelStatus[4] = $langs->transnoentitiesnoconv(
"PropalStatusBilled");
3371 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv(
"PropalStatusDraftShort");
3372 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv(
"PropalStatusValidatedShort");
3373 $this->labelStatusShort[2] = $langs->transnoentitiesnoconv(
"PropalStatusSignedShort");
3374 $this->labelStatusShort[3] = $langs->transnoentitiesnoconv(
"PropalStatusNotSignedShort");
3375 $this->labelStatusShort[4] = $langs->transnoentitiesnoconv(
"PropalStatusBilledShort");
3379 if ($status == self::STATUS_DRAFT) {
3380 $statusType =
'status0';
3381 } elseif ($status == self::STATUS_VALIDATED) {
3382 $statusType =
'status1';
3383 } elseif ($status == self::STATUS_SIGNED) {
3384 $statusType =
'status4';
3385 } elseif ($status == self::STATUS_NOTSIGNED) {
3386 $statusType =
'status9';
3387 } elseif ($status == self::STATUS_BILLED) {
3388 $statusType =
'status6';
3392 $parameters = array(
'status' => $status,
'mode' => $mode);
3393 $reshook = $hookmanager->executeHooks(
'LibStatut', $parameters, $this);
3396 return $hookmanager->resPrint;
3399 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
3414 global $conf, $langs;
3418 $sql =
"SELECT p.rowid, p.ref, p.datec as datec, p.fin_validite as datefin, p.total_ht";
3419 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal as p";
3420 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3421 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
3422 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3425 $sql .= $clause.
" p.entity IN (".
getEntity(
'propal').
")";
3426 if ($mode ==
'opened') {
3427 $sql .=
" AND p.fk_statut = ".self::STATUS_VALIDATED;
3429 if ($mode ==
'signed') {
3430 $sql .=
" AND p.fk_statut = ".self::STATUS_SIGNED;
3433 $sql .=
" AND p.fk_soc = ".((int) $user->socid);
3438 $langs->load(
"propal");
3443 $label = $labelShort =
'';
3444 if ($mode ==
'opened') {
3445 $delay_warning = $conf->propal->cloture->warning_delay;
3447 $label = $langs->transnoentitiesnoconv(
"PropalsToClose");
3448 $labelShort = $langs->transnoentitiesnoconv(
"ToAcceptRefuse");
3450 if ($mode ==
'signed') {
3451 $delay_warning = $conf->propal->facturation->warning_delay;
3453 $label = $langs->trans(
"PropalsToBill");
3454 $labelShort = $langs->trans(
"ToBill");
3458 $response->warning_delay = $delay_warning / 60 / 60 / 24;
3459 $response->label = $label;
3460 $response->labelShort = $labelShort;
3461 $response->url = DOL_URL_ROOT.
'/comm/propal/list.php?search_status='.$status.
'&mainmenu=commercial&leftmenu=propals';
3462 $response->url_late = DOL_URL_ROOT.
'/comm/propal/list.php?search_status='.$status.
'&mainmenu=commercial&leftmenu=propals&sortfield=p.datep&sortorder=asc';
3466 while ($obj = $this->
db->fetch_object(
$resql)) {
3467 $response->nbtodo++;
3468 $response->total += $obj->total_ht;
3470 if ($mode ==
'opened') {
3471 $datelimit = $this->
db->jdate($obj->datefin);
3472 if ($datelimit < ($now - $delay_warning)) {
3473 $response->nbtodolate++;
3482 $this->error = $this->
db->error();
3497 global $conf, $langs;
3502 $sql =
"SELECT rowid";
3503 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
3504 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
3505 $sql .= $this->
db->plimit(100);
3509 $num_prods = $this->
db->num_rows(
$resql);
3511 while ($i < $num_prods) {
3513 $row = $this->
db->fetch_row(
$resql);
3514 $prodids[$i] = $row[0];
3520 $this->
ref =
'SPECIMEN';
3521 $this->ref_client =
'NEMICEPS';
3522 $this->specimen = 1;
3524 $this->date = time();
3525 $this->fin_validite = $this->date + 3600 * 24 * 30;
3526 $this->cond_reglement_id = 1;
3527 $this->cond_reglement_code =
'RECEP';
3528 $this->mode_reglement_id = 7;
3529 $this->mode_reglement_code =
'CHQ';
3530 $this->availability_id = 1;
3531 $this->availability_code =
'AV_NOW';
3532 $this->demand_reason_id = 1;
3533 $this->demand_reason_code =
'SRC_00';
3534 $this->note_public =
'This is a comment (public)';
3535 $this->note_private =
'This is a comment (private)';
3537 $this->multicurrency_tx = 1;
3538 $this->multicurrency_code = $conf->currency;
3543 while ($xnbp < $nbp) {
3545 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
3547 $line->subprice = 100;
3550 $line->localtax1_tx = 0;
3551 $line->localtax2_tx = 0;
3553 $line->total_ht = 50;
3554 $line->total_ttc = 60;
3555 $line->total_tva = 10;
3556 $line->remise_percent = 50;
3558 $line->total_ht = 100;
3559 $line->total_ttc = 120;
3560 $line->total_tva = 20;
3561 $line->remise_percent = 00;
3564 if ($num_prods > 0) {
3565 $prodid = mt_rand(1, $num_prods);
3566 $line->fk_product = $prodids[$prodid];
3567 $line->product_ref =
'SPECIMEN';
3570 $this->lines[$xnbp] = $line;
3572 $this->total_ht += $line->total_ht;
3573 $this->total_tva += $line->total_tva;
3574 $this->total_ttc += $line->total_ttc;
3591 $this->nb = array();
3594 $sql =
"SELECT count(p.rowid) as nb";
3595 $sql .=
" FROM ".MAIN_DB_PREFIX.
"propal as p";
3596 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON p.fk_soc = s.rowid";
3597 if (empty($user->rights->societe->client->voir) && !$user->socid) {
3598 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3599 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
3602 $sql .=
" ".$clause.
" p.entity IN (".
getEntity(
'propal').
")";
3607 while ($obj = $this->
db->fetch_object(
$resql)) {
3608 $this->nb[
"proposals"] = $obj->nb;
3614 $this->error = $this->
db->error();
3629 global $conf, $langs;
3630 $langs->load(
"propal");
3632 $classname = $conf->global->PROPALE_ADDON;
3634 if (!empty($classname)) {
3637 $file = $classname.
".php";
3640 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
3641 foreach ($dirmodels as $reldir) {
3645 $mybool |= @include_once $dir.$file;
3653 $obj =
new $classname();
3655 $numref = $obj->getNextValue($soc, $this);
3657 if ($numref !=
"") {
3660 $this->error = $obj->error;
3665 $langs->load(
"errors");
3666 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Proposal"));
3682 public function getNomUrl($withpicto = 0, $option =
'', $get_params =
'', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = -1)
3684 global $langs, $conf, $user, $hookmanager;
3686 if (!empty($conf->dol_no_mouse_hover)) {
3694 if ($user->rights->propal->lire) {
3695 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Proposal").
'</u>';
3696 if (isset($this->statut)) {
3697 $label .=
' '.$this->getLibStatut(5);
3699 if (!empty($this->
ref)) {
3700 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
3702 if (!empty($this->ref_client)) {
3703 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.$this->ref_client;
3705 if (!empty($this->total_ht)) {
3706 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3708 if (!empty($this->total_tva)) {
3709 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3711 if (!empty($this->total_ttc)) {
3712 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3714 if (!empty($this->date)) {
3715 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
3717 if (!empty($this->delivery_date)) {
3718 $label .=
'<br><b>'.$langs->trans(
'DeliveryDate').
':</b> '.
dol_print_date($this->delivery_date,
'dayhour');
3721 if ($option ==
'') {
3722 $url = DOL_URL_ROOT.
'/comm/propal/card.php?id='.$this->
id.$get_params;
3723 } elseif ($option ==
'compta') {
3724 $url = DOL_URL_ROOT.
'/comm/propal/card.php?id='.$this->
id.$get_params;
3725 } elseif ($option ==
'expedition') {
3726 $url = DOL_URL_ROOT.
'/expedition/propal.php?id='.$this->
id.$get_params;
3727 } elseif ($option ==
'document') {
3728 $url = DOL_URL_ROOT.
'/comm/propal/document.php?id='.$this->
id.$get_params;
3731 if ($option !=
'nolink') {
3733 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3734 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
3735 $add_save_lastsearch_values = 1;
3737 if ($add_save_lastsearch_values) {
3738 $url .=
'&save_lastsearch_values=1';
3744 if (empty($notooltip) && $user->rights->propal->lire) {
3745 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3746 $label = $langs->trans(
"Proposal");
3747 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
3749 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
3750 $linkclose .=
' class="classfortooltip"';
3753 $linkstart =
'<a href="'.$url.
'"';
3754 $linkstart .= $linkclose.
'>';
3757 $result .= $linkstart;
3759 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
3761 if ($withpicto != 2) {
3762 $result .= $this->ref;
3764 $result .= $linkend;
3766 if ($addlinktonotes >= 0) {
3769 if ($addlinktonotes == 0) {
3770 if (!empty($this->note_private) || !empty($this->note_public)) {
3771 $txttoshow = $langs->trans(
'ViewPrivateNote');
3773 } elseif ($addlinktonotes == 1) {
3774 if (!empty($this->note_private)) {
3777 } elseif ($addlinktonotes == 2) {
3778 if (!empty($this->note_public)) {
3781 } elseif ($addlinktonotes == 3) {
3782 if ($user->socid > 0) {
3783 if (!empty($this->note_public)) {
3787 if (!empty($this->note_public)) {
3790 if (!empty($this->note_private)) {
3791 if (!empty($txttoshow)) {
3792 $txttoshow .=
'<br><br>';
3800 $result .=
' <span class="note inline-block">';
3801 $result .=
'<a href="'.DOL_URL_ROOT.
'/comm/propal/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($txttoshow).
'">';
3804 $result .=
'</span>';
3809 $hookmanager->initHooks(array($this->element .
'dao'));
3810 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
3811 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
3813 $result = $hookmanager->resPrint;
3815 $result .= $hookmanager->resPrint;
3842 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
3844 global $conf, $langs;
3846 $langs->load(
"propale");
3847 $outputlangs->load(
"products");
3852 if ($this->model_pdf) {
3853 $modele = $this->model_pdf;
3854 } elseif (!empty($conf->global->PROPALE_ADDON_PDF)) {
3855 $modele = $conf->global->PROPALE_ADDON_PDF;
3859 $modelpath =
"core/modules/propale/doc/";
3861 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3907 public $element =
'propaldet';
3912 public $table_element =
'propaldet';
3918 public $fk_parent_line;
3936 public $vat_src_code;
3939 public $remise_percent;
3940 public $fk_remise_except;
3944 public $fk_fournprice;
3949 public $special_code;
3954 public $info_bits = 0;
3983 public $product_ref;
3998 public $product_label;
4003 public $product_desc;
4009 public $product_tobatch;
4015 public $product_barcode;
4017 public $localtax1_tx;
4018 public $localtax2_tx;
4019 public $localtax1_type;
4020 public $localtax2_type;
4021 public $total_localtax1;
4022 public $total_localtax2;
4027 public $skip_update_total;
4030 public $fk_multicurrency;
4031 public $multicurrency_code;
4032 public $multicurrency_subprice;
4033 public $multicurrency_total_ht;
4034 public $multicurrency_total_tva;
4035 public $multicurrency_total_ttc;
4056 $sql =
'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.vat_src_code, pd.tva_tx,';
4057 $sql .=
' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
4058 $sql .=
' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,';
4059 $sql .=
' pd.fk_unit,';
4060 $sql .=
' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
4061 $sql .=
' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
4062 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
4063 $sql .=
' pd.date_start, pd.date_end, pd.product_type';
4064 $sql .=
' FROM '.MAIN_DB_PREFIX.
'propaldet as pd';
4065 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON pd.fk_product = p.rowid';
4066 $sql .=
' WHERE pd.rowid = '.((int) $rowid);
4068 $result = $this->
db->query($sql);
4070 $objp = $this->
db->fetch_object($result);
4073 $this->
id = $objp->rowid;
4074 $this->
rowid = $objp->rowid;
4075 $this->fk_propal = $objp->fk_propal;
4076 $this->fk_parent_line = $objp->fk_parent_line;
4077 $this->label = $objp->custom_label;
4078 $this->desc = $objp->description;
4079 $this->qty = $objp->qty;
4080 $this->
price = $objp->price;
4081 $this->subprice = $objp->subprice;
4082 $this->vat_src_code = $objp->vat_src_code;
4083 $this->tva_tx = $objp->tva_tx;
4084 $this->remise = $objp->remise;
4085 $this->remise_percent = $objp->remise_percent;
4086 $this->fk_remise_except = $objp->fk_remise_except;
4087 $this->fk_product = $objp->fk_product;
4088 $this->info_bits = $objp->info_bits;
4090 $this->total_ht = $objp->total_ht;
4091 $this->total_tva = $objp->total_tva;
4092 $this->total_ttc = $objp->total_ttc;
4094 $this->fk_fournprice = $objp->fk_fournprice;
4096 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4097 $this->pa_ht = $marginInfos[0];
4098 $this->marge_tx = $marginInfos[1];
4099 $this->marque_tx = $marginInfos[2];
4101 $this->special_code = $objp->special_code;
4102 $this->product_type = $objp->product_type;
4103 $this->rang = $objp->rang;
4105 $this->
ref = $objp->product_ref;
4106 $this->product_ref = $objp->product_ref;
4107 $this->libelle = $objp->product_label;
4108 $this->product_label = $objp->product_label;
4109 $this->product_desc = $objp->product_desc;
4110 $this->fk_unit = $objp->fk_unit;
4112 $this->date_start = $this->
db->jdate($objp->date_start);
4113 $this->date_end = $this->
db->jdate($objp->date_end);
4116 $this->fk_multicurrency = $objp->fk_multicurrency;
4117 $this->multicurrency_code = $objp->multicurrency_code;
4118 $this->multicurrency_subprice = $objp->multicurrency_subprice;
4119 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4120 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4121 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4125 $this->
db->free($result);
4144 global $conf, $user;
4148 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
4150 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4153 if (empty($this->tva_tx)) {
4156 if (empty($this->localtax1_tx)) {
4157 $this->localtax1_tx = 0;
4159 if (empty($this->localtax2_tx)) {
4160 $this->localtax2_tx = 0;
4162 if (empty($this->localtax1_type)) {
4163 $this->localtax1_type = 0;
4165 if (empty($this->localtax2_type)) {
4166 $this->localtax2_type = 0;
4168 if (empty($this->total_localtax1)) {
4169 $this->total_localtax1 = 0;
4171 if (empty($this->total_localtax2)) {
4172 $this->total_localtax2 = 0;
4174 if (empty($this->rang)) {
4177 if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
4178 $this->remise_percent = 0;
4180 if (empty($this->info_bits)) {
4181 $this->info_bits = 0;
4183 if (empty($this->special_code)) {
4184 $this->special_code = 0;
4186 if (empty($this->fk_parent_line)) {
4187 $this->fk_parent_line = 0;
4189 if (empty($this->fk_fournprice)) {
4190 $this->fk_fournprice = 0;
4192 if (!is_numeric($this->qty)) {
4195 if (empty($this->pa_ht)) {
4198 if (empty($this->multicurrency_subprice)) {
4199 $this->multicurrency_subprice = 0;
4201 if (empty($this->multicurrency_total_ht)) {
4202 $this->multicurrency_total_ht = 0;
4204 if (empty($this->multicurrency_total_tva)) {
4205 $this->multicurrency_total_tva = 0;
4207 if (empty($this->multicurrency_total_ttc)) {
4208 $this->multicurrency_total_ttc = 0;
4212 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4213 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
4216 $this->pa_ht = $result;
4221 if ($this->product_type < 0) {
4228 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'propaldet';
4229 $sql .=
' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
4230 $sql .=
' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4231 $sql .=
' subprice, remise_percent, ';
4232 $sql .=
' info_bits, ';
4233 $sql .=
' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
4234 $sql .=
' fk_unit,';
4235 $sql .=
' date_start, date_end';
4236 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
4237 $sql .=
" VALUES (".$this->fk_propal.
",";
4238 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
4239 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
4240 $sql .=
" '".$this->db->escape($this->desc).
"',";
4241 $sql .=
" ".($this->fk_product ?
"'".$this->db->escape($this->fk_product).
"'" :
"null").
",";
4242 $sql .=
" '".$this->db->escape($this->product_type).
"',";
4243 $sql .=
" ".($this->fk_remise_except ?
"'".$this->db->escape($this->fk_remise_except).
"'" :
"null").
",";
4244 $sql .=
" ".price2num($this->qty,
'MS').
",";
4245 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
4246 $sql .=
" ".price2num($this->tva_tx).
",";
4247 $sql .=
" ".price2num($this->localtax1_tx).
",";
4248 $sql .=
" ".price2num($this->localtax2_tx).
",";
4249 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
4250 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
4251 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice,
'MU') :
"null").
",";
4252 $sql .=
" ".price2num($this->remise_percent, 3).
",";
4253 $sql .=
" ".(isset($this->info_bits) ? ((int) $this->info_bits) :
"null").
",";
4254 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
4255 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
4256 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
4257 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
4258 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
4259 $sql .=
" ".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null").
",";
4260 $sql .=
" ".(isset($this->pa_ht) ?
"'".price2num($this->pa_ht).
"'" :
"null").
",";
4261 $sql .=
' '.((int) $this->special_code).
',';
4262 $sql .=
' '.((int) $this->rang).
',';
4263 $sql .=
' '.(empty($this->fk_unit) ?
'NULL' : ((int) $this->fk_unit)).
',';
4264 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
4265 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4266 $sql .=
", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) :
'null');
4267 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
4268 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
4269 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
4270 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
4271 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
4274 dol_syslog(get_class($this).
'::insert', LOG_DEBUG);
4277 $this->
rowid = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'propaldet');
4280 $this->
id = $this->rowid;
4287 if (!$error && !$notrigger) {
4289 $result = $this->
call_trigger(
'LINEPROPAL_INSERT', $user);
4291 $this->
db->rollback();
4297 $this->
db->commit();
4300 $this->error = $this->
db->error().
" sql=".$sql;
4301 $this->
db->rollback();
4313 public function delete(
User $user, $notrigger = 0)
4322 $result = $this->
call_trigger(
'LINEPROPAL_DELETE', $user);
4330 $sql =
"DELETE FROM " . MAIN_DB_PREFIX .
"propaldet WHERE rowid = " . ((int) $this->
rowid);
4331 dol_syslog(
"PropaleLigne::delete", LOG_DEBUG);
4332 if ($this->
db->query($sql)) {
4335 $this->
id = $this->rowid;
4339 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
4343 $this->error = $this->
db->error() .
" sql=" . $sql;
4349 $this->
db->rollback();
4352 $this->
db->commit();
4365 global $conf, $user;
4369 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
4371 if (empty($this->
id) && !empty($this->
rowid)) {
4372 $this->
id = $this->rowid;
4376 if (empty($this->tva_tx)) {
4379 if (empty($this->localtax1_tx)) {
4380 $this->localtax1_tx = 0;
4382 if (empty($this->localtax2_tx)) {
4383 $this->localtax2_tx = 0;
4385 if (empty($this->total_localtax1)) {
4386 $this->total_localtax1 = 0;
4388 if (empty($this->total_localtax2)) {
4389 $this->total_localtax2 = 0;
4391 if (empty($this->localtax1_type)) {
4392 $this->localtax1_type = 0;
4394 if (empty($this->localtax2_type)) {
4395 $this->localtax2_type = 0;
4397 if (empty($this->marque_tx)) {
4398 $this->marque_tx = 0;
4400 if (empty($this->marge_tx)) {
4401 $this->marge_tx = 0;
4403 if (empty($this->
price)) {
4406 if (empty($this->remise_percent)) {
4407 $this->remise_percent = 0;
4409 if (empty($this->info_bits)) {
4410 $this->info_bits = 0;
4412 if (empty($this->special_code)) {
4413 $this->special_code = 0;
4415 if (empty($this->fk_parent_line)) {
4416 $this->fk_parent_line = 0;
4418 if (empty($this->fk_fournprice)) {
4419 $this->fk_fournprice = 0;
4421 if (empty($this->subprice)) {
4422 $this->subprice = 0;
4424 if (empty($this->pa_ht)) {
4429 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4430 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
4433 $this->pa_ht = $result;
4440 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
4441 $sql .=
" description='".$this->db->escape($this->desc).
"'";
4442 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
4443 $sql .=
", product_type=".$this->product_type;
4444 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->vat_src_code).
"'";
4445 $sql .=
", tva_tx='".price2num($this->tva_tx).
"'";
4446 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
4447 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
4448 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
4449 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
4450 $sql .=
", qty='".price2num($this->qty).
"'";
4451 $sql .=
", subprice=".price2num($this->subprice).
"";
4452 $sql .=
", remise_percent=".price2num($this->remise_percent).
"";
4454 $sql .=
", remise=".(float)
price2num($this->remise).
"";
4455 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
4456 if (empty($this->skip_update_total)) {
4457 $sql .=
", total_ht=".price2num($this->total_ht).
"";
4458 $sql .=
", total_tva=".price2num($this->total_tva).
"";
4459 $sql .=
", total_ttc=".price2num($this->total_ttc).
"";
4460 $sql .=
", total_localtax1=".price2num($this->total_localtax1).
"";
4461 $sql .=
", total_localtax2=".price2num($this->total_localtax2).
"";
4463 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null");
4464 $sql .=
", buy_price_ht=".price2num($this->pa_ht);
4465 if (strlen($this->special_code)) {
4466 $sql .=
", special_code=".$this->special_code;
4468 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
4469 if (!empty($this->rang)) {
4470 $sql .=
", rang=".((int) $this->rang);
4472 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
4473 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
4474 $sql .=
", fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
4477 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice).
"";
4478 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht).
"";
4479 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva).
"";
4480 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).
"";
4482 $sql .=
" WHERE rowid = ".((int) $this->
id);
4484 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
4494 if (!$error && !$notrigger) {
4496 $result = $this->
call_trigger(
'LINEPROPAL_MODIFY', $user);
4498 $this->
db->rollback();
4504 $this->
db->commit();
4507 $this->error = $this->
db->error();
4508 $this->
db->rollback();
4526 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"propaldet SET";
4527 $sql .=
" total_ht=".price2num($this->total_ht,
'MT').
"";
4528 $sql .=
",total_tva=".price2num($this->total_tva,
'MT').
"";
4529 $sql .=
",total_ttc=".price2num($this->total_ttc,
'MT').
"";
4530 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
4532 dol_syslog(
"PropaleLigne::update_total", LOG_DEBUG);
4536 $this->
db->commit();
4539 $this->error = $this->
db->error();
4540 $this->
db->rollback();