| Total Complexity | 580 |
| Total Lines | 3934 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Propal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Propal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class Propal extends CommonObject |
||
| 53 | { |
||
| 54 | use CommonIncoterm; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string code |
||
| 58 | */ |
||
| 59 | public $code = ""; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string ID to identify managed object |
||
| 63 | */ |
||
| 64 | public $element = 'propal'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string Name of table without prefix where object is stored |
||
| 68 | */ |
||
| 69 | public $table_element = 'propal'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int Name of subtable line |
||
| 73 | */ |
||
| 74 | public $table_element_line = 'propaldet'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string Fieldname with ID of parent key if this field has a parent |
||
| 78 | */ |
||
| 79 | public $fk_element = 'fk_propal'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 83 | */ |
||
| 84 | public $picto = 'propal'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | public $ismultientitymanaged = 1; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * 0=Default, 1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user |
||
| 94 | * @var integer |
||
| 95 | */ |
||
| 96 | public $restrictiononfksoc = 1; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | protected $table_ref_field = 'ref'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * ID of the client |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | public $socid; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * ID of the contact |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | public $contactid; |
||
| 114 | public $author; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Ref from thirdparty |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | public $ref_client; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Status of the quote |
||
| 124 | * @var int |
||
| 125 | * @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED |
||
| 126 | */ |
||
| 127 | public $statut; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Status of the quote |
||
| 131 | * @var int |
||
| 132 | * @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED |
||
| 133 | */ |
||
| 134 | public $status; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @deprecated |
||
| 138 | * @see $date_creation |
||
| 139 | */ |
||
| 140 | public $datec; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var integer|string $date_creation; |
||
| 144 | */ |
||
| 145 | public $date_creation; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @deprecated |
||
| 149 | * @see $date_validation |
||
| 150 | */ |
||
| 151 | public $datev; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var integer|string $date_validation; |
||
| 155 | */ |
||
| 156 | public $date_validation; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var integer|string $date_signature; |
||
| 160 | */ |
||
| 161 | public $date_signature; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var User $user_signature |
||
| 165 | */ |
||
| 166 | public $user_signature; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var integer|string date of the quote; |
||
| 170 | */ |
||
| 171 | public $date; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @deprecated |
||
| 175 | * @see $date |
||
| 176 | */ |
||
| 177 | public $datep; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var int Date expected for delivery |
||
| 181 | * @deprecated |
||
| 182 | */ |
||
| 183 | public $date_livraison; // deprecated; Use delivery_date instead. |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var integer|string $delivery_date; |
||
| 187 | */ |
||
| 188 | public $delivery_date; // Date expected of shipment (date starting shipment, not the reception that occurs some days after) |
||
| 189 | |||
| 190 | |||
| 191 | public $fin_validite; |
||
| 192 | |||
| 193 | public $user_author_id; |
||
| 194 | public $user_valid_id; |
||
| 195 | public $user_close_id; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @deprecated |
||
| 199 | * @see $total_ht |
||
| 200 | */ |
||
| 201 | public $price; |
||
| 202 | /** |
||
| 203 | * @deprecated |
||
| 204 | * @see $total_tva |
||
| 205 | */ |
||
| 206 | public $tva; |
||
| 207 | /** |
||
| 208 | * @deprecated |
||
| 209 | * @see $total_ttc |
||
| 210 | */ |
||
| 211 | public $total; |
||
| 212 | |||
| 213 | public $cond_reglement_code; |
||
| 214 | public $deposit_percent; |
||
| 215 | public $mode_reglement_code; |
||
| 216 | public $remise_percent; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @deprecated |
||
| 220 | */ |
||
| 221 | public $remise; |
||
| 222 | /** |
||
| 223 | * @deprecated |
||
| 224 | */ |
||
| 225 | public $remise_absolue; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @var int ID |
||
| 229 | * @deprecated |
||
| 230 | */ |
||
| 231 | public $fk_address; |
||
| 232 | |||
| 233 | public $address_type; |
||
| 234 | public $address; |
||
| 235 | |||
| 236 | public $availability_id; |
||
| 237 | public $availability_code; |
||
| 238 | |||
| 239 | public $duree_validite; |
||
| 240 | |||
| 241 | public $demand_reason_id; |
||
| 242 | public $demand_reason_code; |
||
| 243 | |||
| 244 | public $warehouse_id; |
||
| 245 | |||
| 246 | public $extraparams = array(); |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @var PropaleLigne[] |
||
| 250 | */ |
||
| 251 | public $lines = array(); |
||
| 252 | public $line; |
||
| 253 | |||
| 254 | public $labelStatus = array(); |
||
| 255 | public $labelStatusShort = array(); |
||
| 256 | |||
| 257 | // Multicurrency |
||
| 258 | /** |
||
| 259 | * @var int ID |
||
| 260 | */ |
||
| 261 | public $fk_multicurrency; |
||
| 262 | |||
| 263 | public $multicurrency_code; |
||
| 264 | public $multicurrency_tx; |
||
| 265 | public $multicurrency_total_ht; |
||
| 266 | public $multicurrency_total_tva; |
||
| 267 | public $multicurrency_total_ttc; |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * 'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password') |
||
| 272 | * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" |
||
| 273 | * 'label' the translation key. |
||
| 274 | * 'enabled' is a condition when the field must be managed. |
||
| 275 | * 'position' is the sort order of field. |
||
| 276 | * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). |
||
| 277 | * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing) |
||
| 278 | * 'noteditable' says if field is not editable (1 or 0) |
||
| 279 | * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created. |
||
| 280 | * 'index' if we want an index in database. |
||
| 281 | * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). |
||
| 282 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. |
||
| 283 | * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8). |
||
| 284 | * 'css' is the CSS style to use on field. For example: 'maxwidth200' |
||
| 285 | * 'help' is a string visible as a tooltip on field |
||
| 286 | * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record |
||
| 287 | * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code. |
||
| 288 | * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") |
||
| 289 | * 'comment' is not used. You can store here any text of your choice. It is not used by application. |
||
| 290 | * |
||
| 291 | * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor. |
||
| 292 | */ |
||
| 293 | |||
| 294 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 295 | /** |
||
| 296 | * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
| 297 | */ |
||
| 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'=>'isModEnabled("societe")', '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), |
||
| 318 | //'remise_absolue' =>array('type'=>'double', 'label'=>'CustomerRelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>115), |
||
| 319 | //'remise' =>array('type'=>'double', 'label'=>'Remise', 'enabled'=>1, 'visible'=>-1, 'position'=>120), |
||
| 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'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>170), |
||
| 331 | 'note_public' =>array('type'=>'html', '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), // deprecated |
||
| 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), |
||
| 351 | ); |
||
| 352 | // END MODULEBUILDER PROPERTIES |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Draft status |
||
| 356 | */ |
||
| 357 | const STATUS_DRAFT = 0; |
||
| 358 | /** |
||
| 359 | * Validated status |
||
| 360 | */ |
||
| 361 | const STATUS_VALIDATED = 1; |
||
| 362 | /** |
||
| 363 | * Signed quote |
||
| 364 | */ |
||
| 365 | const STATUS_SIGNED = 2; |
||
| 366 | /** |
||
| 367 | * Not signed quote |
||
| 368 | */ |
||
| 369 | const STATUS_NOTSIGNED = 3; |
||
| 370 | /** |
||
| 371 | * Billed or processed quote |
||
| 372 | */ |
||
| 373 | const STATUS_BILLED = 4; // Todo rename into STATUS_CLOSE ? |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Constructor |
||
| 378 | * |
||
| 379 | * @param DoliDB $db Database handler |
||
| 380 | * @param int $socid Id third party |
||
| 381 | * @param int $propalid Id proposal |
||
| 382 | */ |
||
| 383 | public function __construct($db, $socid = 0, $propalid = 0) |
||
| 393 | } |
||
| 394 | |||
| 395 | |||
| 396 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 397 | /** |
||
| 398 | * Add line into array ->lines |
||
| 399 | * $this->thirdparty should be loaded |
||
| 400 | * |
||
| 401 | * @param int $idproduct Product Id to add |
||
| 402 | * @param int $qty Quantity |
||
| 403 | * @param int $remise_percent Discount effected on Product |
||
| 404 | * @return int <0 if KO, >0 if OK |
||
| 405 | * |
||
| 406 | * TODO Replace calls to this function by generation objet Ligne |
||
| 407 | */ |
||
| 408 | public function add_product($idproduct, $qty, $remise_percent = 0) |
||
| 409 | { |
||
| 410 | // phpcs:enable |
||
| 411 | global $conf, $mysoc; |
||
| 412 | |||
| 413 | if (!$qty) { |
||
| 414 | $qty = 1; |
||
| 415 | } |
||
| 416 | |||
| 417 | dol_syslog(get_class($this)."::add_product $idproduct, $qty, $remise_percent"); |
||
| 418 | if ($idproduct > 0) { |
||
| 419 | $prod = new Product($this->db); |
||
| 420 | $prod->fetch($idproduct); |
||
| 421 | |||
| 422 | $productdesc = $prod->description; |
||
| 423 | |||
| 424 | $tva_tx = get_default_tva($mysoc, $this->thirdparty, $prod->id); |
||
| 425 | $tva_npr = get_default_npr($mysoc, $this->thirdparty, $prod->id); |
||
| 426 | if (empty($tva_tx)) { |
||
| 427 | $tva_npr = 0; |
||
| 428 | } |
||
| 429 | $vat_src_code = ''; // May be defined into tva_tx |
||
| 430 | |||
| 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); |
||
| 433 | |||
| 434 | // multiprices |
||
| 435 | if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) { |
||
| 436 | $price = $prod->multiprices[$this->thirdparty->price_level]; |
||
| 437 | } else { |
||
| 438 | $price = $prod->price; |
||
| 439 | } |
||
| 440 | |||
| 441 | $line = new PropaleLigne($this->db); |
||
| 442 | |||
| 443 | $line->fk_product = $idproduct; |
||
| 444 | $line->desc = $productdesc; |
||
| 445 | $line->qty = $qty; |
||
| 446 | $line->subprice = $price; |
||
| 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; |
||
| 451 | if ($tva_npr) { |
||
| 452 | $line->info_bits = 1; |
||
| 453 | } |
||
| 454 | |||
| 455 | $this->lines[] = $line; |
||
| 456 | } |
||
| 457 | |||
| 458 | return 1; |
||
| 459 | } |
||
| 460 | |||
| 461 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 462 | /** |
||
| 463 | * Adding line of fixed discount in the proposal in DB |
||
| 464 | * |
||
| 465 | * @param int $idremise Id of fixed discount |
||
| 466 | * @return int >0 if OK, <0 if KO |
||
| 467 | */ |
||
| 468 | public function insert_discount($idremise) |
||
| 469 | { |
||
| 470 | // phpcs:enable |
||
| 471 | global $langs; |
||
| 472 | |||
| 473 | include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; |
||
| 474 | include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php'; |
||
| 475 | |||
| 476 | $this->db->begin(); |
||
| 477 | |||
| 478 | $remise = new DiscountAbsolute($this->db); |
||
| 479 | $result = $remise->fetch($idremise); |
||
| 480 | |||
| 481 | if ($result > 0) { |
||
| 482 | if ($remise->fk_facture) { // Protection against multiple submission |
||
| 483 | $this->error = $langs->trans("ErrorDiscountAlreadyUsed"); |
||
| 484 | $this->db->rollback(); |
||
| 485 | return -5; |
||
| 486 | } |
||
| 487 | |||
| 488 | $line = new PropaleLigne($this->db); |
||
| 489 | |||
| 490 | $this->line->context = $this->context; |
||
| 491 | |||
| 492 | $line->fk_propal = $this->id; |
||
| 493 | $line->fk_remise_except = $remise->id; |
||
| 494 | $line->desc = $remise->description; // Description ligne |
||
| 495 | $line->vat_src_code = $remise->vat_src_code; |
||
| 496 | $line->tva_tx = $remise->tva_tx; |
||
| 497 | $line->subprice = -$remise->amount_ht; |
||
| 498 | $line->fk_product = 0; // Id produit predefined |
||
| 499 | $line->qty = 1; |
||
| 500 | $line->remise_percent = 0; |
||
| 501 | $line->rang = -1; |
||
| 502 | $line->info_bits = 2; |
||
| 503 | |||
| 504 | // TODO deprecated |
||
| 505 | $line->price = -$remise->amount_ht; |
||
| 506 | |||
| 507 | $line->total_ht = -$remise->amount_ht; |
||
| 508 | $line->total_tva = -$remise->amount_tva; |
||
| 509 | $line->total_ttc = -$remise->amount_ttc; |
||
| 510 | |||
| 511 | $result = $line->insert(); |
||
| 512 | if ($result > 0) { |
||
| 513 | $result = $this->update_price(1); |
||
| 514 | if ($result > 0) { |
||
| 515 | $this->db->commit(); |
||
| 516 | return 1; |
||
| 517 | } else { |
||
| 518 | $this->db->rollback(); |
||
| 519 | return -1; |
||
| 520 | } |
||
| 521 | } else { |
||
| 522 | $this->error = $line->error; |
||
| 523 | $this->errors = $line->errors; |
||
| 524 | $this->db->rollback(); |
||
| 525 | return -2; |
||
| 526 | } |
||
| 527 | } else { |
||
| 528 | $this->db->rollback(); |
||
| 529 | return -2; |
||
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Add a proposal line into database (linked to product/service or not) |
||
| 535 | * The parameters are already supposed to be appropriate and with final values to the call |
||
| 536 | * of this method. Also, for the VAT rate, it must have already been defined |
||
| 537 | * by whose calling the method get_default_tva (societe_vendeuse, societe_acheteuse, '' product) |
||
| 538 | * and desc must already have the right value (it's up to the caller to manage multilanguage) |
||
| 539 | * |
||
| 540 | * @param string $desc Description of line |
||
| 541 | * @param float $pu_ht Unit price |
||
| 542 | * @param float $qty Quantity |
||
| 543 | * @param float $txtva Force Vat rate, -1 for auto (Can contain the vat_src_code too with syntax '9.9 (CODE)') |
||
| 544 | * @param float $txlocaltax1 Local tax 1 rate (deprecated, use instead txtva with code inside) |
||
| 545 | * @param float $txlocaltax2 Local tax 2 rate (deprecated, use instead txtva with code inside) |
||
| 546 | * @param int $fk_product Product/Service ID predefined |
||
| 547 | * @param float $remise_percent Pourcentage de remise de la ligne |
||
| 548 | * @param string $price_base_type HT or TTC |
||
| 549 | * @param float $pu_ttc Prix unitaire TTC |
||
| 550 | * @param int $info_bits Bits for type of lines |
||
| 551 | * @param int $type Type of line (0=product, 1=service). Not used if fk_product is defined, the type of product is used. |
||
| 552 | * @param int $rang Position of line |
||
| 553 | * @param int $special_code Special code (also used by externals modules!) |
||
| 554 | * @param int $fk_parent_line Id of parent line |
||
| 555 | * @param int $fk_fournprice Id supplier price |
||
| 556 | * @param int $pa_ht Buying price without tax |
||
| 557 | * @param string $label ??? |
||
| 558 | * @param int $date_start Start date of the line |
||
| 559 | * @param int $date_end End date of the line |
||
| 560 | * @param array $array_options extrafields array |
||
| 561 | * @param string $fk_unit Code of the unit to use. Null to use the default one |
||
| 562 | * @param string $origin Depend on global conf MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION can be 'orderdet', 'propaldet'..., else 'order','propal,'.... |
||
| 563 | * @param int $origin_id Depend on global conf MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION can be Id of origin object (aka line id), else object id |
||
| 564 | * @param double $pu_ht_devise Unit price in currency |
||
| 565 | * @param int $fk_remise_except Id discount if line is from a discount |
||
| 566 | * @param int $noupdateafterinsertline No update after insert of line |
||
| 567 | * @return int >0 if OK, <0 if KO |
||
| 568 | * @see add_product() |
||
| 569 | */ |
||
| 570 | 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) |
||
| 571 | { |
||
| 572 | global $mysoc, $conf, $langs; |
||
| 573 | |||
| 574 | 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); |
||
| 575 | |||
| 576 | if ($this->statut == self::STATUS_DRAFT) { |
||
| 577 | include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; |
||
| 578 | |||
| 579 | // Clean parameters |
||
| 580 | if (empty($remise_percent)) { |
||
| 581 | $remise_percent = 0; |
||
| 582 | } |
||
| 583 | if (empty($qty)) { |
||
| 584 | $qty = 0; |
||
| 585 | } |
||
| 586 | if (empty($info_bits)) { |
||
| 587 | $info_bits = 0; |
||
| 588 | } |
||
| 589 | if (empty($rang)) { |
||
| 590 | $rang = 0; |
||
| 591 | } |
||
| 592 | if (empty($fk_parent_line) || $fk_parent_line < 0) { |
||
| 593 | $fk_parent_line = 0; |
||
| 594 | } |
||
| 595 | |||
| 596 | $remise_percent = price2num($remise_percent); |
||
| 597 | $qty = price2num($qty); |
||
| 598 | $pu_ht = price2num($pu_ht); |
||
| 599 | $pu_ht_devise = price2num($pu_ht_devise); |
||
| 600 | $pu_ttc = price2num($pu_ttc); |
||
| 601 | if (!preg_match('/\((.*)\)/', $txtva)) { |
||
| 602 | $txtva = price2num($txtva); // $txtva can have format '5,1' or '5.1' or '5.1(XXX)', we must clean only if '5,1' |
||
| 603 | } |
||
| 604 | $txlocaltax1 = price2num($txlocaltax1); |
||
| 605 | $txlocaltax2 = price2num($txlocaltax2); |
||
| 606 | $pa_ht = price2num($pa_ht); |
||
| 607 | if ($price_base_type == 'HT') { |
||
| 608 | $pu = $pu_ht; |
||
| 609 | } else { |
||
| 610 | $pu = $pu_ttc; |
||
| 611 | } |
||
| 612 | |||
| 613 | // Check parameters |
||
| 614 | if ($type < 0) { |
||
| 615 | return -1; |
||
| 616 | } |
||
| 617 | |||
| 618 | if ($date_start && $date_end && $date_start > $date_end) { |
||
| 619 | $langs->load("errors"); |
||
| 620 | $this->error = $langs->trans('ErrorStartDateGreaterEnd'); |
||
| 621 | return -1; |
||
| 622 | } |
||
| 623 | |||
| 624 | $this->db->begin(); |
||
| 625 | |||
| 626 | $product_type = $type; |
||
| 627 | if (!empty($fk_product) && $fk_product > 0) { |
||
| 628 | $product = new Product($this->db); |
||
| 629 | $result = $product->fetch($fk_product); |
||
| 630 | $product_type = $product->type; |
||
| 631 | |||
| 632 | if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_PROPOSAL) && $product_type == 0 && $product->stock_reel < $qty) { |
||
| 633 | $langs->load("errors"); |
||
| 634 | $this->error = $langs->trans('ErrorStockIsNotEnoughToAddProductOnProposal', $product->ref); |
||
| 635 | $this->db->rollback(); |
||
| 636 | return -3; |
||
| 637 | } |
||
| 638 | } |
||
| 639 | |||
| 640 | // Calcul du total TTC et de la TVA pour la ligne a partir de |
||
| 641 | // qty, pu, remise_percent et txtva |
||
| 642 | // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker |
||
| 643 | // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. |
||
| 644 | |||
| 645 | $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc); |
||
| 646 | |||
| 647 | // Clean vat code |
||
| 648 | $reg = array(); |
||
| 649 | $vat_src_code = ''; |
||
| 650 | $reg = array(); |
||
| 651 | if (preg_match('/\((.*)\)/', $txtva, $reg)) { |
||
| 652 | $vat_src_code = $reg[1]; |
||
| 653 | $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. |
||
| 654 | } |
||
| 655 | |||
| 656 | $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); |
||
| 657 | |||
| 658 | $total_ht = $tabprice[0]; |
||
| 659 | $total_tva = $tabprice[1]; |
||
| 660 | $total_ttc = $tabprice[2]; |
||
| 661 | $total_localtax1 = $tabprice[9]; |
||
| 662 | $total_localtax2 = $tabprice[10]; |
||
| 663 | $pu_ht = $tabprice[3]; |
||
| 664 | $pu_tva = $tabprice[4]; |
||
| 665 | $pu_ttc = $tabprice[5]; |
||
| 666 | |||
| 667 | // MultiCurrency |
||
| 668 | $multicurrency_total_ht = $tabprice[16]; |
||
| 669 | $multicurrency_total_tva = $tabprice[17]; |
||
| 670 | $multicurrency_total_ttc = $tabprice[18]; |
||
| 671 | $pu_ht_devise = $tabprice[19]; |
||
| 672 | |||
| 673 | // Rang to use |
||
| 674 | $ranktouse = $rang; |
||
| 675 | if ($ranktouse == -1) { |
||
| 676 | $rangmax = $this->line_max($fk_parent_line); |
||
| 677 | $ranktouse = $rangmax + 1; |
||
| 678 | } |
||
| 679 | |||
| 680 | // TODO A virer |
||
| 681 | // Anciens indicateurs: $price, $remise (a ne plus utiliser) |
||
| 682 | $price = $pu; |
||
| 683 | $remise = 0; |
||
| 684 | if ($remise_percent > 0) { |
||
| 685 | $remise = round(($pu * $remise_percent / 100), 2); |
||
| 686 | $price = $pu - $remise; |
||
| 687 | } |
||
| 688 | |||
| 689 | // Insert line |
||
| 690 | $this->line = new PropaleLigne($this->db); |
||
| 691 | |||
| 692 | $this->line->context = $this->context; |
||
| 693 | |||
| 694 | $this->line->fk_propal = $this->id; |
||
| 695 | $this->line->label = $label; |
||
| 696 | $this->line->desc = $desc; |
||
| 697 | $this->line->qty = $qty; |
||
| 698 | |||
| 699 | $this->line->vat_src_code = $vat_src_code; |
||
| 700 | $this->line->tva_tx = $txtva; |
||
| 701 | $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0); |
||
| 702 | $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0); |
||
| 703 | $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]; |
||
| 704 | $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]; |
||
| 705 | $this->line->fk_product = $fk_product; |
||
| 706 | $this->line->product_type = $type; |
||
| 707 | $this->line->fk_remise_except = $fk_remise_except; |
||
| 708 | $this->line->remise_percent = $remise_percent; |
||
| 709 | $this->line->subprice = $pu_ht; |
||
| 710 | $this->line->rang = $ranktouse; |
||
| 711 | $this->line->info_bits = $info_bits; |
||
| 712 | $this->line->total_ht = $total_ht; |
||
| 713 | $this->line->total_tva = $total_tva; |
||
| 714 | $this->line->total_localtax1 = $total_localtax1; |
||
| 715 | $this->line->total_localtax2 = $total_localtax2; |
||
| 716 | $this->line->total_ttc = $total_ttc; |
||
| 717 | $this->line->special_code = $special_code; |
||
| 718 | $this->line->fk_parent_line = $fk_parent_line; |
||
| 719 | $this->line->fk_unit = $fk_unit; |
||
| 720 | |||
| 721 | $this->line->date_start = $date_start; |
||
| 722 | $this->line->date_end = $date_end; |
||
| 723 | |||
| 724 | $this->line->fk_fournprice = $fk_fournprice; |
||
| 725 | $this->line->pa_ht = $pa_ht; |
||
| 726 | |||
| 727 | $this->line->origin_id = $origin_id; |
||
| 728 | $this->line->origin = $origin; |
||
| 729 | |||
| 730 | // Multicurrency |
||
| 731 | $this->line->fk_multicurrency = $this->fk_multicurrency; |
||
| 732 | $this->line->multicurrency_code = $this->multicurrency_code; |
||
| 733 | $this->line->multicurrency_subprice = $pu_ht_devise; |
||
| 734 | $this->line->multicurrency_total_ht = $multicurrency_total_ht; |
||
| 735 | $this->line->multicurrency_total_tva = $multicurrency_total_tva; |
||
| 736 | $this->line->multicurrency_total_ttc = $multicurrency_total_ttc; |
||
| 737 | |||
| 738 | // Mise en option de la ligne |
||
| 739 | if (empty($qty) && empty($special_code)) { |
||
| 740 | $this->line->special_code = 3; |
||
| 741 | } |
||
| 742 | |||
| 743 | // TODO deprecated |
||
| 744 | $this->line->price = $price; |
||
| 745 | |||
| 746 | if (is_array($array_options) && count($array_options) > 0) { |
||
| 747 | $this->line->array_options = $array_options; |
||
| 748 | } |
||
| 749 | |||
| 750 | $result = $this->line->insert(); |
||
| 751 | if ($result > 0) { |
||
| 752 | // Reorder if child line |
||
| 753 | if (!empty($fk_parent_line)) { |
||
| 754 | $this->line_order(true, 'DESC'); |
||
| 755 | } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { // Update all rank of all other lines |
||
| 756 | $linecount = count($this->lines); |
||
| 757 | for ($ii = $ranktouse; $ii <= $linecount; $ii++) { |
||
| 758 | $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); |
||
| 759 | } |
||
| 760 | } |
||
| 761 | |||
| 762 | // Mise a jour informations denormalisees au niveau de la propale meme |
||
| 763 | if (empty($noupdateafterinsertline)) { |
||
| 764 | $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. |
||
| 765 | } |
||
| 766 | |||
| 767 | if ($result > 0) { |
||
| 768 | $this->db->commit(); |
||
| 769 | return $this->line->id; |
||
| 770 | } else { |
||
| 771 | $this->error = $this->db->error(); |
||
| 772 | $this->db->rollback(); |
||
| 773 | return -1; |
||
| 774 | } |
||
| 775 | } else { |
||
| 776 | $this->error = $this->line->error; |
||
| 777 | $this->errors = $this->line->errors; |
||
| 778 | $this->db->rollback(); |
||
| 779 | return -2; |
||
| 780 | } |
||
| 781 | } else { |
||
| 782 | dol_syslog(get_class($this)."::addline status of proposal must be Draft to allow use of ->addline()", LOG_ERR); |
||
| 783 | return -3; |
||
| 784 | } |
||
| 785 | } |
||
| 786 | |||
| 787 | |||
| 788 | /** |
||
| 789 | * Update a proposal line |
||
| 790 | * |
||
| 791 | * @param int $rowid Id of line |
||
| 792 | * @param float $pu Unit price (HT or TTC depending on price_base_type) |
||
| 793 | * @param float $qty Quantity |
||
| 794 | * @param float $remise_percent Discount on line |
||
| 795 | * @param float $txtva VAT Rate (Can be '1.23' or '1.23 (ABC)') |
||
| 796 | * @param float $txlocaltax1 Local tax 1 rate |
||
| 797 | * @param float $txlocaltax2 Local tax 2 rate |
||
| 798 | * @param string $desc Description |
||
| 799 | * @param string $price_base_type HT or TTC |
||
| 800 | * @param int $info_bits Miscellaneous informations |
||
| 801 | * @param int $special_code Special code (also used by externals modules!) |
||
| 802 | * @param int $fk_parent_line Id of parent line (0 in most cases, used by modules adding sublevels into lines). |
||
| 803 | * @param int $skip_update_total Keep fields total_xxx to 0 (used for special lines by some modules) |
||
| 804 | * @param int $fk_fournprice Id of origin supplier price |
||
| 805 | * @param int $pa_ht Price (without tax) of product when it was bought |
||
| 806 | * @param string $label ??? |
||
| 807 | * @param int $type 0/1=Product/service |
||
| 808 | * @param int $date_start Start date of the line |
||
| 809 | * @param int $date_end End date of the line |
||
| 810 | * @param array $array_options extrafields array |
||
| 811 | * @param string $fk_unit Code of the unit to use. Null to use the default one |
||
| 812 | * @param double $pu_ht_devise Unit price in currency |
||
| 813 | * @param int $notrigger disable line update trigger |
||
| 814 | * @param integer $rang line rank |
||
| 815 | * @return int 0 if OK, <0 if KO |
||
| 816 | */ |
||
| 817 | 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) |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | |||
| 982 | /** |
||
| 983 | * Delete detail line |
||
| 984 | * |
||
| 985 | * @param int $lineid Id of line to delete |
||
| 986 | * @param int $id Id of object (for a check) |
||
| 987 | * @return int >0 if OK, <0 if KO |
||
| 988 | */ |
||
| 989 | public function deleteline($lineid, $id = 0) |
||
| 990 | { |
||
| 991 | global $user; |
||
| 992 | |||
| 993 | if ($this->statut == self::STATUS_DRAFT) { |
||
| 994 | $this->db->begin(); |
||
| 995 | |||
| 996 | $line = new PropaleLigne($this->db); |
||
| 997 | |||
| 998 | $line->context = $this->context; |
||
| 999 | |||
| 1000 | // Load data |
||
| 1001 | $line->fetch($lineid); |
||
| 1002 | |||
| 1003 | if ($id > 0 && $line->fk_propal != $id) { |
||
| 1004 | $this->error = 'ErrorLineIDDoesNotMatchWithObjectID'; |
||
| 1005 | return -1; |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | // Memorize previous line for triggers |
||
| 1009 | $staticline = clone $line; |
||
| 1010 | $line->oldline = $staticline; |
||
| 1011 | |||
| 1012 | if ($line->delete($user) > 0) { |
||
| 1013 | $this->update_price(1); |
||
| 1014 | |||
| 1015 | $this->db->commit(); |
||
| 1016 | return 1; |
||
| 1017 | } else { |
||
| 1018 | $this->error = $line->error; |
||
| 1019 | $this->errors = $line->errors; |
||
| 1020 | $this->db->rollback(); |
||
| 1021 | return -1; |
||
| 1022 | } |
||
| 1023 | } else { |
||
| 1024 | $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus'; |
||
| 1025 | return -2; |
||
| 1026 | } |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Create commercial proposal into database |
||
| 1032 | * this->ref can be set or empty. If empty, we will use "(PROVid)" |
||
| 1033 | * |
||
| 1034 | * @param User $user User that create |
||
| 1035 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 1036 | * @return int <0 if KO, >=0 if OK |
||
| 1037 | */ |
||
| 1038 | public function create($user, $notrigger = 0) |
||
| 1347 | } |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * Load an object from its id and create a new one in database |
||
| 1352 | * |
||
| 1353 | * @param User $user User making the clone |
||
| 1354 | * @param int $socid Id of thirdparty |
||
| 1355 | * @param int $forceentity Entity id to force |
||
| 1356 | * @param bool $update_prices [=false] Update prices if true |
||
| 1357 | * @return int New id of clone |
||
| 1358 | */ |
||
| 1359 | public function createFromClone(User $user, $socid = 0, $forceentity = null, $update_prices = false) |
||
| 1360 | { |
||
| 1361 | global $conf, $hookmanager, $mysoc; |
||
| 1362 | |||
| 1363 | dol_include_once('/projet/class/project.class.php'); |
||
| 1364 | |||
| 1365 | $error = 0; |
||
| 1366 | $now = dol_now(); |
||
| 1367 | |||
| 1368 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1369 | |||
| 1370 | $object = new self($this->db); |
||
| 1371 | |||
| 1372 | $this->db->begin(); |
||
| 1373 | |||
| 1374 | // Load source object |
||
| 1375 | $object->fetch($this->id); |
||
| 1376 | |||
| 1377 | $objsoc = new Societe($this->db); |
||
| 1378 | |||
| 1379 | // Change socid if needed |
||
| 1380 | if (!empty($socid) && $socid != $object->socid) { |
||
| 1381 | if ($objsoc->fetch($socid) > 0) { |
||
| 1382 | $object->socid = $objsoc->id; |
||
| 1383 | $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0); |
||
| 1384 | $object->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : null); |
||
| 1385 | $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0); |
||
| 1386 | $object->fk_delivery_address = ''; |
||
| 1387 | |||
| 1388 | /*if (isModEnabled('project')) |
||
| 1389 | { |
||
| 1390 | $project = new Project($db); |
||
| 1391 | if ($this->fk_project > 0 && $project->fetch($this->fk_project)) { |
||
| 1392 | if ($project->socid <= 0) $clonedObj->fk_project = $this->fk_project; |
||
| 1393 | else $clonedObj->fk_project = ''; |
||
| 1394 | } else { |
||
| 1395 | $clonedObj->fk_project = ''; |
||
| 1396 | } |
||
| 1397 | }*/ |
||
| 1398 | $object->fk_project = ''; // A cloned proposal is set by default to no project. |
||
| 1399 | } |
||
| 1400 | |||
| 1401 | // reset ref_client |
||
| 1402 | $object->ref_client = ''; |
||
| 1403 | |||
| 1404 | // TODO Change product price if multi-prices |
||
| 1405 | } else { |
||
| 1406 | $objsoc->fetch($object->socid); |
||
| 1407 | } |
||
| 1408 | |||
| 1409 | // update prices |
||
| 1410 | if ($update_prices === true) { |
||
| 1411 | if ($objsoc->id > 0 && !empty($object->lines)) { |
||
| 1412 | if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) { |
||
| 1413 | // If price per customer |
||
| 1414 | require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php'; |
||
| 1415 | } |
||
| 1416 | |||
| 1417 | foreach ($object->lines as $line) { |
||
| 1418 | if ($line->fk_product > 0) { |
||
| 1419 | $prod = new Product($this->db); |
||
| 1420 | $res = $prod->fetch($line->fk_product); |
||
| 1421 | if ($res > 0) { |
||
| 1422 | $pu_ht = $prod->price; |
||
| 1423 | $tva_tx = get_default_tva($mysoc, $objsoc, $prod->id); |
||
| 1424 | $remise_percent = $objsoc->remise_percent; |
||
| 1425 | |||
| 1426 | if (!empty($conf->global->PRODUIT_MULTIPRICES) && $objsoc->price_level > 0) { |
||
| 1427 | $pu_ht = $prod->multiprices[$objsoc->price_level]; |
||
| 1428 | if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { // using this option is a bug. kept for backward compatibility |
||
| 1429 | if (isset($prod->multiprices_tva_tx[$objsoc->price_level])) { |
||
| 1430 | $tva_tx = $prod->multiprices_tva_tx[$objsoc->price_level]; |
||
| 1431 | } |
||
| 1432 | } |
||
| 1433 | } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) { |
||
| 1434 | $prodcustprice = new Productcustomerprice($this->db); |
||
| 1435 | $filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $objsoc->id); |
||
| 1436 | $result = $prodcustprice->fetchAll('', '', 0, 0, $filter); |
||
| 1437 | if ($result) { |
||
| 1438 | // If there is some prices specific to the customer |
||
| 1439 | if (count($prodcustprice->lines) > 0) { |
||
| 1440 | $pu_ht = price($prodcustprice->lines[0]->price); |
||
| 1441 | $tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx); |
||
| 1442 | if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) { |
||
| 1443 | $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')'; |
||
| 1444 | } |
||
| 1445 | } |
||
| 1446 | } |
||
| 1447 | } |
||
| 1448 | |||
| 1449 | $line->subprice = $pu_ht; |
||
| 1450 | $line->tva_tx = $tva_tx; |
||
| 1451 | $line->remise_percent = $remise_percent; |
||
| 1452 | } |
||
| 1453 | } |
||
| 1454 | } |
||
| 1455 | } |
||
| 1456 | } |
||
| 1457 | |||
| 1458 | $object->id = 0; |
||
| 1459 | $object->ref = ''; |
||
| 1460 | $object->entity = (!empty($forceentity) ? $forceentity : $object->entity); |
||
| 1461 | $object->statut = self::STATUS_DRAFT; |
||
| 1462 | |||
| 1463 | // Clear fields |
||
| 1464 | $object->user_author = $user->id; |
||
| 1465 | $object->user_valid = 0; |
||
| 1466 | $object->date = $now; |
||
| 1467 | $object->datep = $now; // deprecated |
||
| 1468 | $object->fin_validite = $object->date + ($object->duree_validite * 24 * 3600); |
||
| 1469 | if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) { |
||
| 1470 | $object->ref_client = ''; |
||
| 1471 | } |
||
| 1472 | if ($conf->global->MAIN_DONT_KEEP_NOTE_ON_CLONING == 1) { |
||
| 1473 | $object->note_private = ''; |
||
| 1474 | $object->note_public = ''; |
||
| 1475 | } |
||
| 1476 | // Create clone |
||
| 1477 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 1478 | $result = $object->create($user); |
||
| 1479 | if ($result < 0) { |
||
| 1480 | $this->error = $object->error; |
||
| 1481 | $this->errors = array_merge($this->errors, $object->errors); |
||
| 1482 | $error++; |
||
| 1483 | } |
||
| 1484 | |||
| 1485 | if (!$error) { |
||
| 1486 | // copy internal contacts |
||
| 1487 | if ($object->copy_linked_contact($this, 'internal') < 0) { |
||
| 1488 | $error++; |
||
| 1489 | } |
||
| 1490 | } |
||
| 1491 | |||
| 1492 | if (!$error) { |
||
| 1493 | // copy external contacts if same company |
||
| 1494 | if ($this->socid == $object->socid) { |
||
| 1495 | if ($object->copy_linked_contact($this, 'external') < 0) { |
||
| 1496 | $error++; |
||
| 1497 | } |
||
| 1498 | } |
||
| 1499 | } |
||
| 1500 | |||
| 1501 | if (!$error) { |
||
| 1502 | // Hook of thirdparty module |
||
| 1503 | if (is_object($hookmanager)) { |
||
| 1504 | $parameters = array('objFrom'=>$this, 'clonedObj'=>$object); |
||
| 1505 | $action = ''; |
||
| 1506 | $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 1507 | if ($reshook < 0) { |
||
| 1508 | $this->errors += $hookmanager->errors; |
||
| 1509 | $this->error = $hookmanager->error; |
||
| 1510 | $error++; |
||
| 1511 | } |
||
| 1512 | } |
||
| 1513 | } |
||
| 1514 | |||
| 1515 | unset($object->context['createfromclone']); |
||
| 1516 | |||
| 1517 | // End |
||
| 1518 | if (!$error) { |
||
| 1519 | $this->db->commit(); |
||
| 1520 | return $object->id; |
||
| 1521 | } else { |
||
| 1522 | $this->db->rollback(); |
||
| 1523 | return -1; |
||
| 1524 | } |
||
| 1525 | } |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * Load a proposal from database. Get also lines. |
||
| 1529 | * |
||
| 1530 | * @param int $rowid Id of object to load |
||
| 1531 | * @param string $ref Ref of proposal |
||
| 1532 | * @param string $ref_ext Ref ext of proposal |
||
| 1533 | * @param int $forceentity Entity id to force when searching on ref or ref_ext |
||
| 1534 | * @return int >0 if OK, <0 if KO |
||
| 1535 | */ |
||
| 1536 | public function fetch($rowid, $ref = '', $ref_ext = '', $forceentity = 0) |
||
| 1537 | { |
||
| 1538 | $sql = "SELECT p.rowid, p.ref, p.entity, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc"; |
||
| 1539 | $sql .= ", p.total_ttc, p.total_tva, p.localtax1, p.localtax2, p.total_ht"; |
||
| 1540 | $sql .= ", p.datec"; |
||
| 1541 | $sql .= ", p.date_signature as dates"; |
||
| 1542 | $sql .= ", p.date_valid as datev"; |
||
| 1543 | $sql .= ", p.datep as dp"; |
||
| 1544 | $sql .= ", p.fin_validite as dfv"; |
||
| 1545 | $sql .= ", p.date_livraison as delivery_date"; |
||
| 1546 | $sql .= ", p.model_pdf, p.last_main_doc, p.ref_client, ref_ext, p.extraparams"; |
||
| 1547 | $sql .= ", p.note_private, p.note_public"; |
||
| 1548 | $sql .= ", p.fk_projet as fk_project, p.fk_statut"; |
||
| 1549 | $sql .= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture"; |
||
| 1550 | $sql .= ", p.fk_delivery_address"; |
||
| 1551 | $sql .= ", p.fk_availability"; |
||
| 1552 | $sql .= ", p.fk_input_reason"; |
||
| 1553 | $sql .= ", p.fk_cond_reglement"; |
||
| 1554 | $sql .= ", p.fk_mode_reglement"; |
||
| 1555 | $sql .= ', p.fk_account'; |
||
| 1556 | $sql .= ", p.fk_shipping_method"; |
||
| 1557 | $sql .= ", p.fk_warehouse"; |
||
| 1558 | $sql .= ", p.fk_incoterms, p.location_incoterms"; |
||
| 1559 | $sql .= ", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc"; |
||
| 1560 | $sql .= ", p.tms as date_modification"; |
||
| 1561 | $sql .= ", i.libelle as label_incoterms"; |
||
| 1562 | $sql .= ", c.label as statut_label"; |
||
| 1563 | $sql .= ", ca.code as availability_code, ca.label as availability"; |
||
| 1564 | $sql .= ", dr.code as demand_reason_code, dr.label as demand_reason"; |
||
| 1565 | $sql .= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc, p.deposit_percent"; |
||
| 1566 | $sql .= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement"; |
||
| 1567 | $sql .= " FROM ".MAIN_DB_PREFIX."propal as p"; |
||
| 1568 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_propalst as c ON p.fk_statut = c.id'; |
||
| 1569 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as cp ON p.fk_mode_reglement = cp.id AND cp.entity IN ('.getEntity('c_paiement').')'; |
||
| 1570 | $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').')'; |
||
| 1571 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_availability as ca ON p.fk_availability = ca.rowid'; |
||
| 1572 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_input_reason as dr ON p.fk_input_reason = dr.rowid'; |
||
| 1573 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON p.fk_incoterms = i.rowid'; |
||
| 1574 | |||
| 1575 | if (!empty($ref)) { |
||
| 1576 | if (!empty($forceentity)) { |
||
| 1577 | $sql .= " WHERE p.entity = ".(int) $forceentity; // Check only the current entity because we may have the same reference in several entities |
||
| 1578 | } else { |
||
| 1579 | $sql .= " WHERE p.entity IN (".getEntity('propal').")"; |
||
| 1580 | } |
||
| 1581 | $sql .= " AND p.ref='".$this->db->escape($ref)."'"; |
||
| 1582 | } else { |
||
| 1583 | // Dont't use entity if you use rowid |
||
| 1584 | $sql .= " WHERE p.rowid = ".((int) $rowid); |
||
| 1585 | } |
||
| 1586 | |||
| 1587 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
| 1588 | $resql = $this->db->query($sql); |
||
| 1589 | if ($resql) { |
||
| 1590 | if ($this->db->num_rows($resql)) { |
||
| 1591 | $obj = $this->db->fetch_object($resql); |
||
| 1592 | |||
| 1593 | $this->id = $obj->rowid; |
||
| 1594 | $this->entity = $obj->entity; |
||
| 1595 | |||
| 1596 | $this->ref = $obj->ref; |
||
| 1597 | $this->ref_client = $obj->ref_client; |
||
| 1598 | $this->ref_ext = $obj->ref_ext; |
||
| 1599 | $this->remise = $obj->remise; |
||
| 1600 | $this->remise_percent = $obj->remise_percent; |
||
| 1601 | $this->remise_absolue = $obj->remise_absolue; |
||
| 1602 | $this->total = $obj->total_ttc; // TODO deprecated |
||
| 1603 | $this->total_ttc = $obj->total_ttc; |
||
| 1604 | $this->total_ht = $obj->total_ht; |
||
| 1605 | $this->total_tva = $obj->total_tva; |
||
| 1606 | $this->total_localtax1 = $obj->localtax1; |
||
| 1607 | $this->total_localtax2 = $obj->localtax2; |
||
| 1608 | |||
| 1609 | $this->socid = $obj->fk_soc; |
||
| 1610 | $this->thirdparty = null; // Clear if another value was already set by fetch_thirdparty |
||
| 1611 | |||
| 1612 | $this->fk_project = $obj->fk_project; |
||
| 1613 | $this->project = null; // Clear if another value was already set by fetch_projet |
||
| 1614 | |||
| 1615 | $this->model_pdf = $obj->model_pdf; |
||
| 1616 | $this->modelpdf = $obj->model_pdf; // deprecated |
||
| 1617 | $this->last_main_doc = $obj->last_main_doc; |
||
| 1618 | $this->note = $obj->note_private; // TODO deprecated |
||
| 1619 | $this->note_private = $obj->note_private; |
||
| 1620 | $this->note_public = $obj->note_public; |
||
| 1621 | |||
| 1622 | $this->status = (int) $obj->fk_statut; |
||
| 1623 | $this->statut = $this->status; // deprecated |
||
| 1624 | $this->statut_libelle = $obj->statut_label; |
||
| 1625 | |||
| 1626 | $this->datec = $this->db->jdate($obj->datec); // TODO deprecated |
||
| 1627 | $this->datev = $this->db->jdate($obj->datev); // TODO deprecated |
||
| 1628 | $this->date_creation = $this->db->jdate($obj->datec); //Creation date |
||
| 1629 | $this->date_validation = $this->db->jdate($obj->datev); //Validation date |
||
| 1630 | $this->date_modification = $this->db->jdate($obj->date_modification); // tms |
||
| 1631 | $this->date_signature = $this->db->jdate($obj->dates); // Signature date |
||
| 1632 | $this->date = $this->db->jdate($obj->dp); // Proposal date |
||
| 1633 | $this->datep = $this->db->jdate($obj->dp); // deprecated |
||
| 1634 | $this->fin_validite = $this->db->jdate($obj->dfv); |
||
| 1635 | $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated |
||
| 1636 | $this->delivery_date = $this->db->jdate($obj->delivery_date); |
||
| 1637 | $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null; |
||
| 1638 | $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse : null; |
||
| 1639 | $this->availability_id = $obj->fk_availability; |
||
| 1640 | $this->availability_code = $obj->availability_code; |
||
| 1641 | $this->availability = $obj->availability; |
||
| 1642 | $this->demand_reason_id = $obj->fk_input_reason; |
||
| 1643 | $this->demand_reason_code = $obj->demand_reason_code; |
||
| 1644 | $this->demand_reason = $obj->demand_reason; |
||
| 1645 | $this->fk_address = $obj->fk_delivery_address; |
||
| 1646 | |||
| 1647 | $this->mode_reglement_id = $obj->fk_mode_reglement; |
||
| 1648 | $this->mode_reglement_code = $obj->mode_reglement_code; |
||
| 1649 | $this->mode_reglement = $obj->mode_reglement; |
||
| 1650 | $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account : null; |
||
| 1651 | $this->cond_reglement_id = $obj->fk_cond_reglement; |
||
| 1652 | $this->cond_reglement_code = $obj->cond_reglement_code; |
||
| 1653 | $this->cond_reglement = $obj->cond_reglement; |
||
| 1654 | $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc; |
||
| 1655 | $this->deposit_percent = $obj->deposit_percent; |
||
| 1656 | |||
| 1657 | $this->extraparams = (array) json_decode($obj->extraparams, true); |
||
| 1658 | |||
| 1659 | $this->user_author_id = $obj->fk_user_author; |
||
| 1660 | $this->user_valid_id = $obj->fk_user_valid; |
||
| 1661 | $this->user_close_id = $obj->fk_user_cloture; |
||
| 1662 | |||
| 1663 | //Incoterms |
||
| 1664 | $this->fk_incoterms = $obj->fk_incoterms; |
||
| 1665 | $this->location_incoterms = $obj->location_incoterms; |
||
| 1666 | $this->label_incoterms = $obj->label_incoterms; |
||
| 1667 | |||
| 1668 | // Multicurrency |
||
| 1669 | $this->fk_multicurrency = $obj->fk_multicurrency; |
||
| 1670 | $this->multicurrency_code = $obj->multicurrency_code; |
||
| 1671 | $this->multicurrency_tx = $obj->multicurrency_tx; |
||
| 1672 | $this->multicurrency_total_ht = $obj->multicurrency_total_ht; |
||
| 1673 | $this->multicurrency_total_tva = $obj->multicurrency_total_tva; |
||
| 1674 | $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc; |
||
| 1675 | |||
| 1676 | if ($obj->fk_statut == self::STATUS_DRAFT) { |
||
| 1677 | $this->brouillon = 1; |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | // Retrieve all extrafield |
||
| 1681 | // fetch optionals attributes and labels |
||
| 1682 | $this->fetch_optionals(); |
||
| 1683 | |||
| 1684 | $this->db->free($resql); |
||
| 1685 | |||
| 1686 | $this->lines = array(); |
||
| 1687 | |||
| 1688 | // Lines |
||
| 1689 | $result = $this->fetch_lines(); |
||
| 1690 | if ($result < 0) { |
||
| 1691 | return -3; |
||
| 1692 | } |
||
| 1693 | |||
| 1694 | return 1; |
||
| 1695 | } |
||
| 1696 | |||
| 1697 | $this->error = "Record Not Found"; |
||
| 1698 | return 0; |
||
| 1699 | } else { |
||
| 1700 | $this->error = $this->db->lasterror(); |
||
| 1701 | return -1; |
||
| 1702 | } |
||
| 1703 | } |
||
| 1704 | |||
| 1705 | /** |
||
| 1706 | * Update database |
||
| 1707 | * |
||
| 1708 | * @param User $user User that modify |
||
| 1709 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 1710 | * @return int <0 if KO, >0 if OK |
||
| 1711 | */ |
||
| 1712 | public function update(User $user, $notrigger = 0) |
||
| 1713 | { |
||
| 1714 | global $conf; |
||
| 1715 | |||
| 1716 | $error = 0; |
||
| 1717 | |||
| 1718 | // Clean parameters |
||
| 1719 | if (isset($this->ref)) { |
||
| 1720 | $this->ref = trim($this->ref); |
||
| 1721 | } |
||
| 1722 | if (isset($this->ref_client)) { |
||
| 1723 | $this->ref_client = trim($this->ref_client); |
||
| 1724 | } |
||
| 1725 | if (isset($this->note) || isset($this->note_private)) { |
||
| 1726 | $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note)); |
||
| 1727 | } |
||
| 1728 | if (isset($this->note_public)) { |
||
| 1729 | $this->note_public = trim($this->note_public); |
||
| 1730 | } |
||
| 1731 | if (isset($this->model_pdf)) { |
||
| 1732 | $this->model_pdf = trim($this->model_pdf); |
||
| 1733 | } |
||
| 1734 | if (isset($this->import_key)) { |
||
| 1735 | $this->import_key = trim($this->import_key); |
||
| 1736 | } |
||
| 1737 | if (!empty($this->duree_validite) && is_numeric($this->duree_validite)) { |
||
| 1738 | $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600); |
||
| 1739 | } |
||
| 1740 | |||
| 1741 | // Check parameters |
||
| 1742 | // Put here code to add control on parameters values |
||
| 1743 | |||
| 1744 | // Update request |
||
| 1745 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET"; |
||
| 1746 | $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").","; |
||
| 1747 | $sql .= " ref_client=".(isset($this->ref_client) ? "'".$this->db->escape($this->ref_client)."'" : "null").","; |
||
| 1748 | $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").","; |
||
| 1749 | $sql .= " fk_soc=".(isset($this->socid) ? $this->socid : "null").","; |
||
| 1750 | $sql .= " datep=".(strval($this->date) != '' ? "'".$this->db->idate($this->date)."'" : 'null').","; |
||
| 1751 | if (!empty($this->fin_validite)) { |
||
| 1752 | $sql .= " fin_validite=".(strval($this->fin_validite) != '' ? "'".$this->db->idate($this->fin_validite)."'" : 'null').","; |
||
| 1753 | } |
||
| 1754 | $sql .= " date_valid=".(strval($this->date_validation) != '' ? "'".$this->db->idate($this->date_validation)."'" : 'null').","; |
||
| 1755 | $sql .= " total_tva=".(isset($this->total_tva) ? $this->total_tva : "null").","; |
||
| 1756 | $sql .= " localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 : "null").","; |
||
| 1757 | $sql .= " localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 : "null").","; |
||
| 1758 | $sql .= " total_ht=".(isset($this->total_ht) ? $this->total_ht : "null").","; |
||
| 1759 | $sql .= " total_ttc=".(isset($this->total_ttc) ? $this->total_ttc : "null").","; |
||
| 1760 | $sql .= " fk_statut=".(isset($this->statut) ? $this->statut : "null").","; |
||
| 1761 | $sql .= " fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id : "null").","; |
||
| 1762 | $sql .= " fk_user_valid=".(isset($this->user_valid) ? $this->user_valid : "null").","; |
||
| 1763 | $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").","; |
||
| 1764 | $sql .= " fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id : "null").","; |
||
| 1765 | $sql .= " deposit_percent=".(!empty($this->deposit_percent) ? "'".$this->db->escape($this->deposit_percent)."'" : "null").","; |
||
| 1766 | $sql .= " fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id : "null").","; |
||
| 1767 | $sql .= " fk_input_reason=".(isset($this->demand_reason_id) ? $this->demand_reason_id : "null").","; |
||
| 1768 | $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").","; |
||
| 1769 | $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").","; |
||
| 1770 | $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").","; |
||
| 1771 | $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null"); |
||
| 1772 | $sql .= " WHERE rowid=".((int) $this->id); |
||
| 1773 | |||
| 1774 | $this->db->begin(); |
||
| 1775 | |||
| 1776 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 1777 | $resql = $this->db->query($sql); |
||
| 1778 | if (!$resql) { |
||
| 1779 | $error++; |
||
| 1780 | $this->errors[] = "Error ".$this->db->lasterror(); |
||
| 1781 | } |
||
| 1782 | |||
| 1783 | if (!$error) { |
||
| 1784 | $result = $this->insertExtraFields(); |
||
| 1785 | if ($result < 0) { |
||
| 1786 | $error++; |
||
| 1787 | } |
||
| 1788 | } |
||
| 1789 | |||
| 1790 | if (!$error && !$notrigger) { |
||
| 1791 | // Call trigger |
||
| 1792 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 1793 | if ($result < 0) { |
||
| 1794 | $error++; |
||
| 1795 | } |
||
| 1796 | // End call triggers |
||
| 1797 | } |
||
| 1798 | |||
| 1799 | // Commit or rollback |
||
| 1800 | if ($error) { |
||
| 1801 | foreach ($this->errors as $errmsg) { |
||
| 1802 | dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); |
||
| 1803 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 1804 | } |
||
| 1805 | $this->db->rollback(); |
||
| 1806 | return -1 * $error; |
||
| 1807 | } else { |
||
| 1808 | $this->db->commit(); |
||
| 1809 | return 1; |
||
| 1810 | } |
||
| 1811 | } |
||
| 1812 | |||
| 1813 | |||
| 1814 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1815 | /** |
||
| 1816 | * Load array lines |
||
| 1817 | * |
||
| 1818 | * @param int $only_product Return only physical products |
||
| 1819 | * @param int $loadalsotranslation Return translation for products |
||
| 1820 | * @param string $filters Filter on other fields |
||
| 1821 | * |
||
| 1822 | * @return int <0 if KO, >0 if OK |
||
| 1823 | */ |
||
| 1824 | public function fetch_lines($only_product = 0, $loadalsotranslation = 0, $filters = '') |
||
| 1825 | { |
||
| 1826 | // phpcs:enable |
||
| 1827 | global $langs, $conf; |
||
| 1828 | |||
| 1829 | $this->lines = array(); |
||
| 1830 | |||
| 1831 | $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,'; |
||
| 1832 | $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,'; |
||
| 1833 | $sql .= ' d.fk_unit,'; |
||
| 1834 | $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,'; |
||
| 1835 | $sql .= ' p.weight, p.weight_units, p.volume, p.volume_units,'; |
||
| 1836 | $sql .= ' d.date_start, d.date_end,'; |
||
| 1837 | $sql .= ' d.fk_multicurrency, d.multicurrency_code, d.multicurrency_subprice, d.multicurrency_total_ht, d.multicurrency_total_tva, d.multicurrency_total_ttc'; |
||
| 1838 | $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as d'; |
||
| 1839 | $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (d.fk_product = p.rowid)'; |
||
| 1840 | $sql .= ' WHERE d.fk_propal = '.((int) $this->id); |
||
| 1841 | if ($only_product) { |
||
| 1842 | $sql .= ' AND p.fk_product_type = 0'; |
||
| 1843 | } |
||
| 1844 | if ($filters) { |
||
| 1845 | $sql .= $filters; |
||
| 1846 | } |
||
| 1847 | $sql .= ' ORDER by d.rang'; |
||
| 1848 | |||
| 1849 | dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG); |
||
| 1850 | $result = $this->db->query($sql); |
||
| 1851 | if ($result) { |
||
| 1852 | require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; |
||
| 1853 | |||
| 1854 | $num = $this->db->num_rows($result); |
||
| 1855 | |||
| 1856 | $i = 0; |
||
| 1857 | while ($i < $num) { |
||
| 1858 | $objp = $this->db->fetch_object($result); |
||
| 1859 | |||
| 1860 | $line = new PropaleLigne($this->db); |
||
| 1861 | |||
| 1862 | $line->rowid = $objp->rowid; //Deprecated |
||
| 1863 | $line->id = $objp->rowid; |
||
| 1864 | $line->fk_propal = $objp->fk_propal; |
||
| 1865 | $line->fk_parent_line = $objp->fk_parent_line; |
||
| 1866 | $line->product_type = $objp->product_type; |
||
| 1867 | $line->label = $objp->custom_label; |
||
| 1868 | $line->desc = $objp->description; // Description ligne |
||
| 1869 | $line->description = $objp->description; // Description ligne |
||
| 1870 | $line->qty = $objp->qty; |
||
| 1871 | $line->vat_src_code = $objp->vat_src_code; |
||
| 1872 | $line->tva_tx = $objp->tva_tx; |
||
| 1873 | $line->localtax1_tx = $objp->localtax1_tx; |
||
| 1874 | $line->localtax2_tx = $objp->localtax2_tx; |
||
| 1875 | $line->localtax1_type = $objp->localtax1_type; |
||
| 1876 | $line->localtax2_type = $objp->localtax2_type; |
||
| 1877 | $line->subprice = $objp->subprice; |
||
| 1878 | $line->fk_remise_except = $objp->fk_remise_except; |
||
| 1879 | $line->remise_percent = $objp->remise_percent; |
||
| 1880 | $line->price = $objp->price; // TODO deprecated |
||
| 1881 | |||
| 1882 | $line->info_bits = $objp->info_bits; |
||
| 1883 | $line->total_ht = $objp->total_ht; |
||
| 1884 | $line->total_tva = $objp->total_tva; |
||
| 1885 | $line->total_localtax1 = $objp->total_localtax1; |
||
| 1886 | $line->total_localtax2 = $objp->total_localtax2; |
||
| 1887 | $line->total_ttc = $objp->total_ttc; |
||
| 1888 | $line->fk_fournprice = $objp->fk_fournprice; |
||
| 1889 | $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht); |
||
| 1890 | $line->pa_ht = $marginInfos[0]; |
||
| 1891 | $line->marge_tx = $marginInfos[1]; |
||
| 1892 | $line->marque_tx = $marginInfos[2]; |
||
| 1893 | $line->special_code = $objp->special_code; |
||
| 1894 | $line->rang = $objp->rang; |
||
| 1895 | |||
| 1896 | $line->fk_product = $objp->fk_product; |
||
| 1897 | |||
| 1898 | $line->ref = $objp->product_ref; // deprecated |
||
| 1899 | $line->libelle = $objp->product_label; // deprecated |
||
| 1900 | |||
| 1901 | $line->product_ref = $objp->product_ref; |
||
| 1902 | $line->product_label = $objp->product_label; |
||
| 1903 | $line->product_desc = $objp->product_desc; // Description produit |
||
| 1904 | $line->product_tobatch = $objp->product_tobatch; |
||
| 1905 | $line->product_barcode = $objp->product_barcode; |
||
| 1906 | |||
| 1907 | $line->fk_product_type = $objp->fk_product_type; // deprecated |
||
| 1908 | $line->fk_unit = $objp->fk_unit; |
||
| 1909 | $line->weight = $objp->weight; |
||
| 1910 | $line->weight_units = $objp->weight_units; |
||
| 1911 | $line->volume = $objp->volume; |
||
| 1912 | $line->volume_units = $objp->volume_units; |
||
| 1913 | |||
| 1914 | $line->date_start = $this->db->jdate($objp->date_start); |
||
| 1915 | $line->date_end = $this->db->jdate($objp->date_end); |
||
| 1916 | |||
| 1917 | // Multicurrency |
||
| 1918 | $line->fk_multicurrency = $objp->fk_multicurrency; |
||
| 1919 | $line->multicurrency_code = $objp->multicurrency_code; |
||
| 1920 | $line->multicurrency_subprice = $objp->multicurrency_subprice; |
||
| 1921 | $line->multicurrency_total_ht = $objp->multicurrency_total_ht; |
||
| 1922 | $line->multicurrency_total_tva = $objp->multicurrency_total_tva; |
||
| 1923 | $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc; |
||
| 1924 | |||
| 1925 | $line->fetch_optionals(); |
||
| 1926 | |||
| 1927 | // multilangs |
||
| 1928 | if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) { |
||
| 1929 | $tmpproduct = new Product($this->db); |
||
| 1930 | $tmpproduct->fetch($objp->fk_product); |
||
| 1931 | $tmpproduct->getMultiLangs(); |
||
| 1932 | |||
| 1933 | $line->multilangs = $tmpproduct->multilangs; |
||
| 1934 | } |
||
| 1935 | |||
| 1936 | $this->lines[$i] = $line; |
||
| 1937 | |||
| 1938 | $i++; |
||
| 1939 | } |
||
| 1940 | |||
| 1941 | $this->db->free($result); |
||
| 1942 | |||
| 1943 | return $num; |
||
| 1944 | } else { |
||
| 1945 | $this->error = $this->db->lasterror(); |
||
| 1946 | return -3; |
||
| 1947 | } |
||
| 1948 | } |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * Set status to validated |
||
| 1952 | * |
||
| 1953 | * @param User $user Object user that validate |
||
| 1954 | * @param int $notrigger 1=Does not execute triggers, 0=execute triggers |
||
| 1955 | * @return int <0 if KO, 0=Nothing done, >=0 if OK |
||
| 1956 | */ |
||
| 1957 | public function valid($user, $notrigger = 0) |
||
| 1958 | { |
||
| 1959 | global $conf; |
||
| 1960 | |||
| 1961 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 1962 | |||
| 1963 | $error = 0; |
||
| 1964 | |||
| 1965 | // Protection |
||
| 1966 | if ($this->statut == self::STATUS_VALIDATED) { |
||
| 1967 | dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING); |
||
| 1968 | return 0; |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->creer)) |
||
| 1972 | || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->validate)))) { |
||
| 1973 | $this->error = 'ErrorPermissionDenied'; |
||
| 1974 | dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR); |
||
| 1975 | return -1; |
||
| 1976 | } |
||
| 1977 | |||
| 1978 | $now = dol_now(); |
||
| 1979 | |||
| 1980 | $this->db->begin(); |
||
| 1981 | |||
| 1982 | // Numbering module definition |
||
| 1983 | $soc = new Societe($this->db); |
||
| 1984 | $soc->fetch($this->socid); |
||
| 1985 | |||
| 1986 | // Define new ref |
||
| 1987 | if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life |
||
| 1988 | $num = $this->getNextNumRef($soc); |
||
| 1989 | } else { |
||
| 1990 | $num = $this->ref; |
||
| 1991 | } |
||
| 1992 | $this->newref = dol_sanitizeFileName($num); |
||
| 1993 | |||
| 1994 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; |
||
| 1995 | $sql .= " SET ref = '".$this->db->escape($num)."',"; |
||
| 1996 | $sql .= " fk_statut = ".self::STATUS_VALIDATED.", date_valid='".$this->db->idate($now)."', fk_user_valid=".((int) $user->id); |
||
| 1997 | $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT; |
||
| 1998 | |||
| 1999 | dol_syslog(get_class($this)."::valid", LOG_DEBUG); |
||
| 2000 | $resql = $this->db->query($sql); |
||
| 2001 | if (!$resql) { |
||
| 2002 | dol_print_error($this->db); |
||
| 2003 | $error++; |
||
| 2004 | } |
||
| 2005 | |||
| 2006 | // Trigger calls |
||
| 2007 | if (!$error && !$notrigger) { |
||
| 2008 | // Call trigger |
||
| 2009 | $result = $this->call_trigger('PROPAL_VALIDATE', $user); |
||
| 2010 | if ($result < 0) { |
||
| 2011 | $error++; |
||
| 2012 | } |
||
| 2013 | // End call triggers |
||
| 2014 | } |
||
| 2015 | |||
| 2016 | if (!$error) { |
||
| 2017 | $this->oldref = $this->ref; |
||
| 2018 | |||
| 2019 | // Rename directory if dir was a temporary ref |
||
| 2020 | if (preg_match('/^[\(]?PROV/i', $this->ref)) { |
||
| 2021 | // Now we rename also files into index |
||
| 2022 | $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)."'"; |
||
| 2023 | $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'propale/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity); |
||
| 2024 | $resql = $this->db->query($sql); |
||
| 2025 | if (!$resql) { |
||
| 2026 | $error++; |
||
| 2027 | $this->error = $this->db->lasterror(); |
||
| 2028 | } |
||
| 2029 | |||
| 2030 | // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments |
||
| 2031 | $oldref = dol_sanitizeFileName($this->ref); |
||
| 2032 | $newref = dol_sanitizeFileName($num); |
||
| 2033 | $dirsource = $conf->propal->multidir_output[$this->entity].'/'.$oldref; |
||
| 2034 | $dirdest = $conf->propal->multidir_output[$this->entity].'/'.$newref; |
||
| 2035 | if (!$error && file_exists($dirsource)) { |
||
| 2036 | dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest); |
||
| 2037 | if (@rename($dirsource, $dirdest)) { |
||
| 2038 | dol_syslog("Rename ok"); |
||
| 2039 | // Rename docs starting with $oldref with $newref |
||
| 2040 | $listoffiles = dol_dir_list($dirdest, 'files', 1, '^'.preg_quote($oldref, '/')); |
||
| 2041 | foreach ($listoffiles as $fileentry) { |
||
| 2042 | $dirsource = $fileentry['name']; |
||
| 2043 | $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource); |
||
| 2044 | $dirsource = $fileentry['path'].'/'.$dirsource; |
||
| 2045 | $dirdest = $fileentry['path'].'/'.$dirdest; |
||
| 2046 | @rename($dirsource, $dirdest); |
||
| 2047 | } |
||
| 2048 | } |
||
| 2049 | } |
||
| 2050 | } |
||
| 2051 | |||
| 2052 | $this->ref = $num; |
||
| 2053 | $this->brouillon = 0; |
||
| 2054 | $this->statut = self::STATUS_VALIDATED; |
||
| 2055 | $this->user_valid_id = $user->id; |
||
| 2056 | $this->datev = $now; |
||
| 2057 | |||
| 2058 | $this->db->commit(); |
||
| 2059 | return 1; |
||
| 2060 | } else { |
||
| 2061 | $this->db->rollback(); |
||
| 2062 | return -1; |
||
| 2063 | } |
||
| 2064 | } |
||
| 2065 | |||
| 2066 | |||
| 2067 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2068 | /** |
||
| 2069 | * Define proposal date |
||
| 2070 | * |
||
| 2071 | * @param User $user Object user that modify |
||
| 2072 | * @param int $date Date |
||
| 2073 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2074 | * @return int <0 if KO, >0 if OK |
||
| 2075 | */ |
||
| 2076 | public function set_date($user, $date, $notrigger = 0) |
||
| 2077 | { |
||
| 2078 | // phpcs:enable |
||
| 2079 | if (empty($date)) { |
||
| 2080 | $this->error = 'ErrorBadParameter'; |
||
| 2081 | dol_syslog(get_class($this)."::set_date ".$this->error, LOG_ERR); |
||
| 2082 | return -1; |
||
| 2083 | } |
||
| 2084 | |||
| 2085 | if (!empty($user->rights->propal->creer)) { |
||
| 2086 | $error = 0; |
||
| 2087 | |||
| 2088 | $this->db->begin(); |
||
| 2089 | |||
| 2090 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET datep = '".$this->db->idate($date)."'"; |
||
| 2091 | $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT; |
||
| 2092 | |||
| 2093 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2094 | $resql = $this->db->query($sql); |
||
| 2095 | if (!$resql) { |
||
| 2096 | $this->errors[] = $this->db->error(); |
||
| 2097 | $error++; |
||
| 2098 | } |
||
| 2099 | |||
| 2100 | if (!$error) { |
||
| 2101 | $this->oldcopy = clone $this; |
||
| 2102 | $this->date = $date; |
||
| 2103 | $this->datep = $date; // deprecated |
||
| 2104 | } |
||
| 2105 | |||
| 2106 | if (!$notrigger && empty($error)) { |
||
| 2107 | // Call trigger |
||
| 2108 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2109 | if ($result < 0) { |
||
| 2110 | $error++; |
||
| 2111 | } |
||
| 2112 | // End call triggers |
||
| 2113 | } |
||
| 2114 | |||
| 2115 | if (!$error) { |
||
| 2116 | $this->db->commit(); |
||
| 2117 | return 1; |
||
| 2118 | } else { |
||
| 2119 | foreach ($this->errors as $errmsg) { |
||
| 2120 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2121 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2122 | } |
||
| 2123 | $this->db->rollback(); |
||
| 2124 | return -1 * $error; |
||
| 2125 | } |
||
| 2126 | } |
||
| 2127 | |||
| 2128 | return -1; |
||
| 2129 | } |
||
| 2130 | |||
| 2131 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2132 | /** |
||
| 2133 | * Define end validity date |
||
| 2134 | * |
||
| 2135 | * @param User $user Object user that modify |
||
| 2136 | * @param int $date_end_validity End of validity date |
||
| 2137 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2138 | * @return int <0 if KO, >0 if OK |
||
| 2139 | */ |
||
| 2140 | public function set_echeance($user, $date_end_validity, $notrigger = 0) |
||
| 2141 | { |
||
| 2142 | // phpcs:enable |
||
| 2143 | if (!empty($user->rights->propal->creer)) { |
||
| 2144 | $error = 0; |
||
| 2145 | |||
| 2146 | $this->db->begin(); |
||
| 2147 | |||
| 2148 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".($date_end_validity != '' ? "'".$this->db->idate($date_end_validity)."'" : 'null'); |
||
| 2149 | $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".((int) self::STATUS_DRAFT); |
||
| 2150 | |||
| 2151 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2152 | $resql = $this->db->query($sql); |
||
| 2153 | if (!$resql) { |
||
| 2154 | $this->errors[] = $this->db->error(); |
||
| 2155 | $error++; |
||
| 2156 | } |
||
| 2157 | |||
| 2158 | |||
| 2159 | if (!$error) { |
||
| 2160 | $this->oldcopy = clone $this; |
||
| 2161 | $this->fin_validite = $date_end_validity; |
||
| 2162 | } |
||
| 2163 | |||
| 2164 | if (!$notrigger && empty($error)) { |
||
| 2165 | // Call trigger |
||
| 2166 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2167 | if ($result < 0) { |
||
| 2168 | $error++; |
||
| 2169 | } |
||
| 2170 | // End call triggers |
||
| 2171 | } |
||
| 2172 | |||
| 2173 | if (!$error) { |
||
| 2174 | $this->db->commit(); |
||
| 2175 | return 1; |
||
| 2176 | } else { |
||
| 2177 | foreach ($this->errors as $errmsg) { |
||
| 2178 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2179 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2180 | } |
||
| 2181 | $this->db->rollback(); |
||
| 2182 | return -1 * $error; |
||
| 2183 | } |
||
| 2184 | } |
||
| 2185 | |||
| 2186 | return -1; |
||
| 2187 | } |
||
| 2188 | |||
| 2189 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2190 | /** |
||
| 2191 | * Set delivery date |
||
| 2192 | * |
||
| 2193 | * @param User $user Object user that modify |
||
| 2194 | * @param int $delivery_date Delivery date |
||
| 2195 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2196 | * @return int <0 if ko, >0 if ok |
||
| 2197 | * @deprecated Use setDeliveryDate |
||
| 2198 | */ |
||
| 2199 | public function set_date_livraison($user, $delivery_date, $notrigger = 0) |
||
| 2200 | { |
||
| 2201 | // phpcs:enable |
||
| 2202 | return $this->setDeliveryDate($user, $delivery_date, $notrigger); |
||
| 2203 | } |
||
| 2204 | |||
| 2205 | /** |
||
| 2206 | * Set delivery date |
||
| 2207 | * |
||
| 2208 | * @param User $user Object user that modify |
||
| 2209 | * @param int $delivery_date Delivery date |
||
| 2210 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2211 | * @return int <0 if ko, >0 if ok |
||
| 2212 | */ |
||
| 2213 | public function setDeliveryDate($user, $delivery_date, $notrigger = 0) |
||
| 2214 | { |
||
| 2215 | if (!empty($user->rights->propal->creer)) { |
||
| 2216 | $error = 0; |
||
| 2217 | |||
| 2218 | $this->db->begin(); |
||
| 2219 | |||
| 2220 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal "; |
||
| 2221 | $sql .= " SET date_livraison = ".($delivery_date != '' ? "'".$this->db->idate($delivery_date)."'" : 'null'); |
||
| 2222 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2223 | |||
| 2224 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2225 | $resql = $this->db->query($sql); |
||
| 2226 | if (!$resql) { |
||
| 2227 | $this->errors[] = $this->db->error(); |
||
| 2228 | $error++; |
||
| 2229 | } |
||
| 2230 | |||
| 2231 | if (!$error) { |
||
| 2232 | $this->oldcopy = clone $this; |
||
| 2233 | $this->date_livraison = $delivery_date; |
||
| 2234 | $this->delivery_date = $delivery_date; |
||
| 2235 | } |
||
| 2236 | |||
| 2237 | if (!$notrigger && empty($error)) { |
||
| 2238 | // Call trigger |
||
| 2239 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2240 | if ($result < 0) { |
||
| 2241 | $error++; |
||
| 2242 | } |
||
| 2243 | // End call triggers |
||
| 2244 | } |
||
| 2245 | |||
| 2246 | if (!$error) { |
||
| 2247 | $this->db->commit(); |
||
| 2248 | return 1; |
||
| 2249 | } else { |
||
| 2250 | foreach ($this->errors as $errmsg) { |
||
| 2251 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2252 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2253 | } |
||
| 2254 | $this->db->rollback(); |
||
| 2255 | return -1 * $error; |
||
| 2256 | } |
||
| 2257 | } |
||
| 2258 | |||
| 2259 | return -1; |
||
| 2260 | } |
||
| 2261 | |||
| 2262 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2263 | /** |
||
| 2264 | * Set delivery |
||
| 2265 | * |
||
| 2266 | * @param User $user Object user that modify |
||
| 2267 | * @param int $id Availability id |
||
| 2268 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2269 | * @return int <0 if KO, >0 if OK |
||
| 2270 | */ |
||
| 2271 | public function set_availability($user, $id, $notrigger = 0) |
||
| 2272 | { |
||
| 2273 | // phpcs:enable |
||
| 2274 | if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) { |
||
| 2275 | $error = 0; |
||
| 2276 | |||
| 2277 | $this->db->begin(); |
||
| 2278 | |||
| 2279 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; |
||
| 2280 | $sql .= " SET fk_availability = ".((int) $id); |
||
| 2281 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2282 | |||
| 2283 | dol_syslog(__METHOD__.' availability('.$id.')', LOG_DEBUG); |
||
| 2284 | $resql = $this->db->query($sql); |
||
| 2285 | if (!$resql) { |
||
| 2286 | $this->errors[] = $this->db->error(); |
||
| 2287 | $error++; |
||
| 2288 | } |
||
| 2289 | |||
| 2290 | if (!$error) { |
||
| 2291 | $this->oldcopy = clone $this; |
||
| 2292 | $this->fk_availability = $id; |
||
| 2293 | $this->availability_id = $id; |
||
| 2294 | } |
||
| 2295 | |||
| 2296 | if (!$notrigger && empty($error)) { |
||
| 2297 | // Call trigger |
||
| 2298 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2299 | if ($result < 0) { |
||
| 2300 | $error++; |
||
| 2301 | } |
||
| 2302 | // End call triggers |
||
| 2303 | } |
||
| 2304 | |||
| 2305 | if (!$error) { |
||
| 2306 | $this->db->commit(); |
||
| 2307 | return 1; |
||
| 2308 | } else { |
||
| 2309 | foreach ($this->errors as $errmsg) { |
||
| 2310 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2311 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2312 | } |
||
| 2313 | $this->db->rollback(); |
||
| 2314 | return -1 * $error; |
||
| 2315 | } |
||
| 2316 | } else { |
||
| 2317 | $error_str = 'Propal status do not meet requirement '.$this->statut; |
||
| 2318 | dol_syslog(__METHOD__.$error_str, LOG_ERR); |
||
| 2319 | $this->error = $error_str; |
||
| 2320 | $this->errors[] = $this->error; |
||
| 2321 | return -2; |
||
| 2322 | } |
||
| 2323 | } |
||
| 2324 | |||
| 2325 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2326 | /** |
||
| 2327 | * Set source of demand |
||
| 2328 | * |
||
| 2329 | * @param User $user Object user that modify |
||
| 2330 | * @param int $id Input reason id |
||
| 2331 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2332 | * @return int <0 if KO, >0 if OK |
||
| 2333 | */ |
||
| 2334 | public function set_demand_reason($user, $id, $notrigger = 0) |
||
| 2335 | { |
||
| 2336 | // phpcs:enable |
||
| 2337 | if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) { |
||
| 2338 | $error = 0; |
||
| 2339 | |||
| 2340 | $this->db->begin(); |
||
| 2341 | |||
| 2342 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal "; |
||
| 2343 | $sql .= " SET fk_input_reason = ".((int) $id); |
||
| 2344 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2345 | |||
| 2346 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2347 | $resql = $this->db->query($sql); |
||
| 2348 | if (!$resql) { |
||
| 2349 | $this->errors[] = $this->db->error(); |
||
| 2350 | $error++; |
||
| 2351 | } |
||
| 2352 | |||
| 2353 | |||
| 2354 | if (!$error) { |
||
| 2355 | $this->oldcopy = clone $this; |
||
| 2356 | $this->fk_input_reason = $id; |
||
| 2357 | $this->demand_reason_id = $id; |
||
| 2358 | } |
||
| 2359 | |||
| 2360 | |||
| 2361 | if (!$notrigger && empty($error)) { |
||
| 2362 | // Call trigger |
||
| 2363 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2364 | if ($result < 0) { |
||
| 2365 | $error++; |
||
| 2366 | } |
||
| 2367 | // End call triggers |
||
| 2368 | } |
||
| 2369 | |||
| 2370 | if (!$error) { |
||
| 2371 | $this->db->commit(); |
||
| 2372 | return 1; |
||
| 2373 | } else { |
||
| 2374 | foreach ($this->errors as $errmsg) { |
||
| 2375 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2376 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2377 | } |
||
| 2378 | $this->db->rollback(); |
||
| 2379 | return -1 * $error; |
||
| 2380 | } |
||
| 2381 | } else { |
||
| 2382 | $error_str = 'Propal status do not meet requirement '.$this->statut; |
||
| 2383 | dol_syslog(__METHOD__.$error_str, LOG_ERR); |
||
| 2384 | $this->error = $error_str; |
||
| 2385 | $this->errors[] = $this->error; |
||
| 2386 | return -2; |
||
| 2387 | } |
||
| 2388 | } |
||
| 2389 | |||
| 2390 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2391 | /** |
||
| 2392 | * Set customer reference number |
||
| 2393 | * |
||
| 2394 | * @param User $user Object user that modify |
||
| 2395 | * @param string $ref_client Customer reference |
||
| 2396 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2397 | * @return int <0 if ko, >0 if ok |
||
| 2398 | */ |
||
| 2399 | public function set_ref_client($user, $ref_client, $notrigger = 0) |
||
| 2400 | { |
||
| 2401 | // phpcs:enable |
||
| 2402 | if (!empty($user->rights->propal->creer)) { |
||
| 2403 | $error = 0; |
||
| 2404 | |||
| 2405 | $this->db->begin(); |
||
| 2406 | |||
| 2407 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'"); |
||
| 2408 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2409 | |||
| 2410 | dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG); |
||
| 2411 | $resql = $this->db->query($sql); |
||
| 2412 | if (!$resql) { |
||
| 2413 | $this->errors[] = $this->db->error(); |
||
| 2414 | $error++; |
||
| 2415 | } |
||
| 2416 | |||
| 2417 | if (!$error) { |
||
| 2418 | $this->oldcopy = clone $this; |
||
| 2419 | $this->ref_client = $ref_client; |
||
| 2420 | } |
||
| 2421 | |||
| 2422 | if (!$notrigger && empty($error)) { |
||
| 2423 | // Call trigger |
||
| 2424 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2425 | if ($result < 0) { |
||
| 2426 | $error++; |
||
| 2427 | } |
||
| 2428 | // End call triggers |
||
| 2429 | } |
||
| 2430 | |||
| 2431 | if (!$error) { |
||
| 2432 | $this->db->commit(); |
||
| 2433 | return 1; |
||
| 2434 | } else { |
||
| 2435 | foreach ($this->errors as $errmsg) { |
||
| 2436 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2437 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2438 | } |
||
| 2439 | $this->db->rollback(); |
||
| 2440 | return -1 * $error; |
||
| 2441 | } |
||
| 2442 | } |
||
| 2443 | |||
| 2444 | return -1; |
||
| 2445 | } |
||
| 2446 | |||
| 2447 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2448 | /** |
||
| 2449 | * Set an overall discount on the proposal |
||
| 2450 | * |
||
| 2451 | * @param User $user Object user that modify |
||
| 2452 | * @param double $remise Amount discount |
||
| 2453 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2454 | * @return int <0 if ko, >0 if ok |
||
| 2455 | */ |
||
| 2456 | public function set_remise_percent($user, $remise, $notrigger = 0) |
||
| 2457 | { |
||
| 2458 | // phpcs:enable |
||
| 2459 | $remise = trim($remise) ?trim($remise) : 0; |
||
| 2460 | |||
| 2461 | if (!empty($user->rights->propal->creer)) { |
||
| 2462 | $remise = price2num($remise, 2); |
||
| 2463 | |||
| 2464 | $error = 0; |
||
| 2465 | |||
| 2466 | $this->db->begin(); |
||
| 2467 | |||
| 2468 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".((float) $remise); |
||
| 2469 | $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT; |
||
| 2470 | |||
| 2471 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2472 | $resql = $this->db->query($sql); |
||
| 2473 | if (!$resql) { |
||
| 2474 | $this->errors[] = $this->db->error(); |
||
| 2475 | $error++; |
||
| 2476 | } |
||
| 2477 | |||
| 2478 | if (!$error) { |
||
| 2479 | $this->oldcopy = clone $this; |
||
| 2480 | $this->remise_percent = $remise; |
||
| 2481 | $this->update_price(1); |
||
| 2482 | } |
||
| 2483 | |||
| 2484 | if (!$notrigger && empty($error)) { |
||
| 2485 | // Call trigger |
||
| 2486 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2487 | if ($result < 0) { |
||
| 2488 | $error++; |
||
| 2489 | } |
||
| 2490 | // End call triggers |
||
| 2491 | } |
||
| 2492 | |||
| 2493 | if (!$error) { |
||
| 2494 | $this->db->commit(); |
||
| 2495 | return 1; |
||
| 2496 | } else { |
||
| 2497 | foreach ($this->errors as $errmsg) { |
||
| 2498 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2499 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2500 | } |
||
| 2501 | $this->db->rollback(); |
||
| 2502 | return -1 * $error; |
||
| 2503 | } |
||
| 2504 | } |
||
| 2505 | |||
| 2506 | return -1; |
||
| 2507 | } |
||
| 2508 | |||
| 2509 | |||
| 2510 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2511 | /** |
||
| 2512 | * Set an absolute overall discount on the proposal |
||
| 2513 | * |
||
| 2514 | * @param User $user Object user that modify |
||
| 2515 | * @param double $remise Amount discount |
||
| 2516 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2517 | * @return int <0 if ko, >0 if ok |
||
| 2518 | */ |
||
| 2519 | public function set_remise_absolue($user, $remise, $notrigger = 0) |
||
| 2520 | { |
||
| 2521 | // phpcs:enable |
||
| 2522 | if (empty($remise)) { |
||
| 2523 | $remise = 0; |
||
| 2524 | } |
||
| 2525 | $remise = price2num($remise); |
||
| 2526 | |||
| 2527 | if (!empty($user->rights->propal->creer)) { |
||
| 2528 | $error = 0; |
||
| 2529 | |||
| 2530 | $this->db->begin(); |
||
| 2531 | |||
| 2532 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; |
||
| 2533 | $sql .= " SET remise_absolue = ".((float) $remise); |
||
| 2534 | $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT; |
||
| 2535 | |||
| 2536 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2537 | $resql = $this->db->query($sql); |
||
| 2538 | if (!$resql) { |
||
| 2539 | $this->errors[] = $this->db->error(); |
||
| 2540 | $error++; |
||
| 2541 | } |
||
| 2542 | |||
| 2543 | if (!$error) { |
||
| 2544 | $this->oldcopy = clone $this; |
||
| 2545 | $this->update_price(1); |
||
| 2546 | } |
||
| 2547 | |||
| 2548 | if (!$notrigger && empty($error)) { |
||
| 2549 | // Call trigger |
||
| 2550 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2551 | if ($result < 0) { |
||
| 2552 | $error++; |
||
| 2553 | } |
||
| 2554 | // End call triggers |
||
| 2555 | } |
||
| 2556 | |||
| 2557 | if (!$error) { |
||
| 2558 | $this->db->commit(); |
||
| 2559 | return 1; |
||
| 2560 | } else { |
||
| 2561 | foreach ($this->errors as $errmsg) { |
||
| 2562 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2563 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2564 | } |
||
| 2565 | $this->db->rollback(); |
||
| 2566 | return -1 * $error; |
||
| 2567 | } |
||
| 2568 | } |
||
| 2569 | |||
| 2570 | return -1; |
||
| 2571 | } |
||
| 2572 | |||
| 2573 | |||
| 2574 | |||
| 2575 | /** |
||
| 2576 | * Reopen the commercial proposal |
||
| 2577 | * |
||
| 2578 | * @param User $user Object user that close |
||
| 2579 | * @param int $status Status |
||
| 2580 | * @param string $note Comment |
||
| 2581 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2582 | * @return int <0 if KO, >0 if OK |
||
| 2583 | */ |
||
| 2584 | public function reopen($user, $status, $note = '', $notrigger = 0) |
||
| 2585 | { |
||
| 2586 | $error = 0; |
||
| 2587 | |||
| 2588 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; |
||
| 2589 | $sql .= " SET fk_statut = ".((int) $status).","; |
||
| 2590 | if (!empty($note)) { |
||
| 2591 | $sql .= " note_private = '".$this->db->escape($note)."',"; |
||
| 2592 | } |
||
| 2593 | $sql .= " date_cloture=NULL, fk_user_cloture=NULL"; |
||
| 2594 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2595 | |||
| 2596 | $this->db->begin(); |
||
| 2597 | |||
| 2598 | dol_syslog(get_class($this)."::reopen", LOG_DEBUG); |
||
| 2599 | $resql = $this->db->query($sql); |
||
| 2600 | if (!$resql) { |
||
| 2601 | $error++; |
||
| 2602 | $this->errors[] = "Error ".$this->db->lasterror(); |
||
| 2603 | } |
||
| 2604 | if (!$error) { |
||
| 2605 | if (!$notrigger) { |
||
| 2606 | // Call trigger |
||
| 2607 | $result = $this->call_trigger('PROPAL_REOPEN', $user); |
||
| 2608 | if ($result < 0) { |
||
| 2609 | $error++; |
||
| 2610 | } |
||
| 2611 | // End call triggers |
||
| 2612 | } |
||
| 2613 | } |
||
| 2614 | |||
| 2615 | // Commit or rollback |
||
| 2616 | if ($error) { |
||
| 2617 | if (!empty($this->errors)) { |
||
| 2618 | foreach ($this->errors as $errmsg) { |
||
| 2619 | dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); |
||
| 2620 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2621 | } |
||
| 2622 | } |
||
| 2623 | $this->db->rollback(); |
||
| 2624 | return -1 * $error; |
||
| 2625 | } else { |
||
| 2626 | $this->statut = $status; |
||
| 2627 | $this->status = $status; |
||
| 2628 | |||
| 2629 | $this->db->commit(); |
||
| 2630 | return 1; |
||
| 2631 | } |
||
| 2632 | } |
||
| 2633 | |||
| 2634 | /** |
||
| 2635 | * Close/set the commercial proposal to status signed or refused (fill also date signature) |
||
| 2636 | * |
||
| 2637 | * @param User $user Object user that close |
||
| 2638 | * @param int $status Status (self::STATUS_BILLED or self::STATUS_REFUSED) |
||
| 2639 | * @param string $note Complete private note with this note |
||
| 2640 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 2641 | * @return int <0 if KO, >0 if OK |
||
| 2642 | */ |
||
| 2643 | public function closeProposal($user, $status, $note = '', $notrigger = 0) |
||
| 2644 | { |
||
| 2645 | global $langs,$conf; |
||
| 2646 | |||
| 2647 | $error = 0; |
||
| 2648 | $now = dol_now(); |
||
| 2649 | |||
| 2650 | $this->db->begin(); |
||
| 2651 | |||
| 2652 | $newprivatenote = dol_concatdesc($this->note_private, $note); |
||
| 2653 | |||
| 2654 | if (empty($conf->global->PROPALE_KEEP_OLD_SIGNATURE_INFO)) { |
||
| 2655 | $date_signature = $now; |
||
| 2656 | $fk_user_signature = $user->id; |
||
| 2657 | } else { |
||
| 2658 | $this->info($this->id); |
||
| 2659 | if (!isset($this->date_signature) || $this->date_signature == '') { |
||
| 2660 | $date_signature = $now; |
||
| 2661 | $fk_user_signature = $user->id; |
||
| 2662 | } else { |
||
| 2663 | $date_signature = $this->date_signature; |
||
| 2664 | $fk_user_signature = $this->user_signature->id; |
||
| 2665 | } |
||
| 2666 | } |
||
| 2667 | |||
| 2668 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; |
||
| 2669 | $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; |
||
| 2670 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2671 | |||
| 2672 | $resql = $this->db->query($sql); |
||
| 2673 | if ($resql) { |
||
| 2674 | // Status self::STATUS_REFUSED by default |
||
| 2675 | $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_CLOSED) ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf; |
||
| 2676 | $trigger_name = 'PROPAL_CLOSE_REFUSED'; // used later in call_trigger() |
||
| 2677 | |||
| 2678 | if ($status == self::STATUS_SIGNED) { // Status self::STATUS_SIGNED |
||
| 2679 | $trigger_name = 'PROPAL_CLOSE_SIGNED'; // used later in call_trigger() |
||
| 2680 | $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_TOBILL) ? $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL : $this->model_pdf; |
||
| 2681 | |||
| 2682 | // The connected company is classified as a client |
||
| 2683 | $soc=new Societe($this->db); |
||
| 2684 | $soc->id = $this->socid; |
||
| 2685 | $result = $soc->set_as_client(); |
||
| 2686 | |||
| 2687 | if ($result < 0) { |
||
| 2688 | $this->error=$this->db->lasterror(); |
||
| 2689 | $this->db->rollback(); |
||
| 2690 | return -2; |
||
| 2691 | } |
||
| 2692 | } |
||
| 2693 | |||
| 2694 | if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { |
||
| 2695 | // Define output language |
||
| 2696 | $outputlangs = $langs; |
||
| 2697 | if (getDolGlobalInt('MAIN_MULTILANGS')) { |
||
| 2698 | $outputlangs = new Translate("", $conf); |
||
| 2699 | $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang); |
||
| 2700 | $outputlangs->setDefaultLang($newlang); |
||
| 2701 | } |
||
| 2702 | |||
| 2703 | |||
| 2704 | $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0); |
||
| 2705 | $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0); |
||
| 2706 | $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0); |
||
| 2707 | |||
| 2708 | //$ret=$object->fetch($id); // Reload to get new records |
||
| 2709 | $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref); |
||
| 2710 | } |
||
| 2711 | |||
| 2712 | if (!$error) { |
||
| 2713 | $this->oldcopy= clone $this; |
||
| 2714 | $this->statut = $status; |
||
| 2715 | $this->status = $status; |
||
| 2716 | $this->date_signature = $date_signature; |
||
| 2717 | $this->note_private = $newprivatenote; |
||
| 2718 | } |
||
| 2719 | |||
| 2720 | if (!$notrigger && empty($error)) { |
||
| 2721 | // Call trigger |
||
| 2722 | $result=$this->call_trigger($trigger_name, $user); |
||
| 2723 | if ($result < 0) { |
||
| 2724 | $error++; |
||
| 2725 | } |
||
| 2726 | // End call triggers |
||
| 2727 | } |
||
| 2728 | |||
| 2729 | if (!$error ) { |
||
| 2730 | $this->db->commit(); |
||
| 2731 | return 1; |
||
| 2732 | } else { |
||
| 2733 | $this->statut = $this->oldcopy->statut; |
||
| 2734 | $this->status = $this->oldcopy->statut; |
||
| 2735 | $this->date_signature = $this->oldcopy->date_signature; |
||
| 2736 | $this->note_private = $this->oldcopy->note_private; |
||
| 2737 | |||
| 2738 | $this->db->rollback(); |
||
| 2739 | return -1; |
||
| 2740 | } |
||
| 2741 | } else { |
||
| 2742 | $this->error = $this->db->lasterror(); |
||
| 2743 | $this->db->rollback(); |
||
| 2744 | return -1; |
||
| 2745 | } |
||
| 2746 | } |
||
| 2747 | |||
| 2748 | /** |
||
| 2749 | * Classify the proposal to status Billed |
||
| 2750 | * |
||
| 2751 | * @param User $user Object user |
||
| 2752 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2753 | * @param string $note Complete private note with this note |
||
| 2754 | * @return int <0 if KO, 0 = nothing done, >0 if OK |
||
| 2755 | */ |
||
| 2756 | public function classifyBilled(User $user, $notrigger = 0, $note = '') |
||
| 2757 | { |
||
| 2758 | global $conf, $langs; |
||
| 2759 | |||
| 2760 | $error = 0; |
||
| 2761 | |||
| 2762 | $now = dol_now(); |
||
| 2763 | $num = 0; |
||
| 2764 | |||
| 2765 | $triggerName = 'PROPAL_CLASSIFY_BILLED'; |
||
| 2766 | |||
| 2767 | $this->db->begin(); |
||
| 2768 | |||
| 2769 | $newprivatenote = dol_concatdesc($this->note_private, $note); |
||
| 2770 | |||
| 2771 | $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_statut = '.self::STATUS_BILLED.", "; |
||
| 2772 | $sql .= " note_private = '".$this->db->escape($newprivatenote)."', date_cloture='".$this->db->idate($now)."', fk_user_cloture=".((int) $user->id); |
||
| 2773 | $sql .= ' WHERE rowid = '.((int) $this->id).' AND fk_statut = '.((int) self::STATUS_SIGNED); |
||
| 2774 | |||
| 2775 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 2776 | $resql = $this->db->query($sql); |
||
| 2777 | if (!$resql) { |
||
| 2778 | $this->errors[] = $this->db->error(); |
||
| 2779 | $error++; |
||
| 2780 | } else { |
||
| 2781 | $num = $this->db->affected_rows($resql); |
||
| 2782 | } |
||
| 2783 | |||
| 2784 | if (!$error) { |
||
| 2785 | $modelpdf = $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf; |
||
| 2786 | |||
| 2787 | if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { |
||
| 2788 | // Define output language |
||
| 2789 | $outputlangs = $langs; |
||
| 2790 | if (getDolGlobalInt('MAIN_MULTILANGS')) { |
||
| 2791 | $outputlangs = new Translate("", $conf); |
||
| 2792 | $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang); |
||
| 2793 | $outputlangs->setDefaultLang($newlang); |
||
| 2794 | } |
||
| 2795 | |||
| 2796 | |||
| 2797 | $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0); |
||
| 2798 | $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0); |
||
| 2799 | $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0); |
||
| 2800 | |||
| 2801 | //$ret=$object->fetch($id); // Reload to get new records |
||
| 2802 | $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref); |
||
| 2803 | } |
||
| 2804 | |||
| 2805 | $this->oldcopy = clone $this; |
||
| 2806 | $this->statut = self::STATUS_BILLED; |
||
| 2807 | $this->date_cloture = $now; |
||
| 2808 | $this->note_private = $newprivatenote; |
||
| 2809 | } |
||
| 2810 | |||
| 2811 | if (!$notrigger && empty($error)) { |
||
| 2812 | // Call trigger |
||
| 2813 | $result = $this->call_trigger($triggerName, $user); |
||
| 2814 | if ($result < 0) { |
||
| 2815 | $error++; |
||
| 2816 | } |
||
| 2817 | // End call triggers |
||
| 2818 | } |
||
| 2819 | |||
| 2820 | if (!$error) { |
||
| 2821 | $this->db->commit(); |
||
| 2822 | return $num; |
||
| 2823 | } else { |
||
| 2824 | foreach ($this->errors as $errmsg) { |
||
| 2825 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2826 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2827 | } |
||
| 2828 | $this->db->rollback(); |
||
| 2829 | return -1 * $error; |
||
| 2830 | } |
||
| 2831 | } |
||
| 2832 | |||
| 2833 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2834 | /** |
||
| 2835 | * Set draft status |
||
| 2836 | * |
||
| 2837 | * @param User $user Object user that modify |
||
| 2838 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 2839 | * @return int <0 if KO, >0 if OK |
||
| 2840 | */ |
||
| 2841 | public function setDraft($user, $notrigger = 0) |
||
| 2842 | { |
||
| 2843 | // phpcs:enable |
||
| 2844 | $error = 0; |
||
| 2845 | |||
| 2846 | // Protection |
||
| 2847 | if ($this->statut <= self::STATUS_DRAFT) { |
||
| 2848 | return 0; |
||
| 2849 | } |
||
| 2850 | |||
| 2851 | dol_syslog(get_class($this)."::setDraft", LOG_DEBUG); |
||
| 2852 | |||
| 2853 | $this->db->begin(); |
||
| 2854 | |||
| 2855 | $sql = "UPDATE ".MAIN_DB_PREFIX."propal"; |
||
| 2856 | $sql .= " SET fk_statut = ".self::STATUS_DRAFT; |
||
| 2857 | $sql .= ", online_sign_ip = NULL , online_sign_name = NULL"; |
||
| 2858 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 2859 | |||
| 2860 | $resql = $this->db->query($sql); |
||
| 2861 | if (!$resql) { |
||
| 2862 | $this->errors[] = $this->db->error(); |
||
| 2863 | $error++; |
||
| 2864 | } |
||
| 2865 | |||
| 2866 | if (!$error) { |
||
| 2867 | $this->oldcopy = clone $this; |
||
| 2868 | } |
||
| 2869 | |||
| 2870 | if (!$notrigger && empty($error)) { |
||
| 2871 | // Call trigger |
||
| 2872 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 2873 | if ($result < 0) { |
||
| 2874 | $error++; |
||
| 2875 | } |
||
| 2876 | // End call triggers |
||
| 2877 | } |
||
| 2878 | |||
| 2879 | if (!$error) { |
||
| 2880 | $this->statut = self::STATUS_DRAFT; |
||
| 2881 | $this->brouillon = 1; |
||
| 2882 | |||
| 2883 | $this->db->commit(); |
||
| 2884 | return 1; |
||
| 2885 | } else { |
||
| 2886 | foreach ($this->errors as $errmsg) { |
||
| 2887 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 2888 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 2889 | } |
||
| 2890 | $this->db->rollback(); |
||
| 2891 | return -1 * $error; |
||
| 2892 | } |
||
| 2893 | } |
||
| 2894 | |||
| 2895 | |||
| 2896 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2897 | /** |
||
| 2898 | * Return list of proposal (eventually filtered on user) into an array |
||
| 2899 | * |
||
| 2900 | * @param int $shortlist 0=Return array[id]=ref, 1=Return array[](id=>id,ref=>ref,name=>name) |
||
| 2901 | * @param int $draft 0=not draft, 1=draft |
||
| 2902 | * @param int $notcurrentuser 0=all user, 1=not current user |
||
| 2903 | * @param int $socid Id third pary |
||
| 2904 | * @param int $limit For pagination |
||
| 2905 | * @param int $offset For pagination |
||
| 2906 | * @param string $sortfield Sort criteria |
||
| 2907 | * @param string $sortorder Sort order |
||
| 2908 | * @return array|int -1 if KO, array with result if OK |
||
| 2909 | */ |
||
| 2910 | public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datep', $sortorder = 'DESC') |
||
| 2911 | { |
||
| 2912 | // phpcs:enable |
||
| 2913 | global $user; |
||
| 2914 | |||
| 2915 | $ga = array(); |
||
| 2916 | |||
| 2917 | $sql = "SELECT s.rowid, s.nom as name, s.client,"; |
||
| 2918 | $sql .= " p.rowid as propalid, p.fk_statut, p.total_ht, p.ref, p.remise, "; |
||
| 2919 | $sql .= " p.datep as dp, p.fin_validite as datelimite"; |
||
| 2920 | if (empty($user->rights->societe->client->voir) && !$socid) { |
||
| 2921 | $sql .= ", sc.fk_soc, sc.fk_user"; |
||
| 2922 | } |
||
| 2923 | $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."c_propalst as c"; |
||
| 2924 | if (empty($user->rights->societe->client->voir) && !$socid) { |
||
| 2925 | $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; |
||
| 2926 | } |
||
| 2927 | $sql .= " WHERE p.entity IN (".getEntity('propal').")"; |
||
| 2928 | $sql .= " AND p.fk_soc = s.rowid"; |
||
| 2929 | $sql .= " AND p.fk_statut = c.id"; |
||
| 2930 | if (empty($user->rights->societe->client->voir) && !$socid) { //restriction |
||
| 2931 | $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id); |
||
| 2932 | } |
||
| 2933 | if ($socid) { |
||
| 2934 | $sql .= " AND s.rowid = ".((int) $socid); |
||
| 2935 | } |
||
| 2936 | if ($draft) { |
||
| 2937 | $sql .= " AND p.fk_statut = ".self::STATUS_DRAFT; |
||
| 2938 | } |
||
| 2939 | if ($notcurrentuser > 0) { |
||
| 2940 | $sql .= " AND p.fk_user_author <> ".((int) $user->id); |
||
| 2941 | } |
||
| 2942 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 2943 | $sql .= $this->db->plimit($limit, $offset); |
||
| 2944 | |||
| 2945 | $result = $this->db->query($sql); |
||
| 2946 | if ($result) { |
||
| 2947 | $num = $this->db->num_rows($result); |
||
| 2948 | if ($num) { |
||
| 2949 | $i = 0; |
||
| 2950 | while ($i < $num) { |
||
| 2951 | $obj = $this->db->fetch_object($result); |
||
| 2952 | |||
| 2953 | if ($shortlist == 1) { |
||
| 2954 | $ga[$obj->propalid] = $obj->ref; |
||
| 2955 | } elseif ($shortlist == 2) { |
||
| 2956 | $ga[$obj->propalid] = $obj->ref.' ('.$obj->name.')'; |
||
| 2957 | } else { |
||
| 2958 | $ga[$i]['id'] = $obj->propalid; |
||
| 2959 | $ga[$i]['ref'] = $obj->ref; |
||
| 2960 | $ga[$i]['name'] = $obj->name; |
||
| 2961 | } |
||
| 2962 | |||
| 2963 | $i++; |
||
| 2964 | } |
||
| 2965 | } |
||
| 2966 | return $ga; |
||
| 2967 | } else { |
||
| 2968 | dol_print_error($this->db); |
||
| 2969 | return -1; |
||
| 2970 | } |
||
| 2971 | } |
||
| 2972 | |||
| 2973 | /** |
||
| 2974 | * Returns an array with the numbers of related invoices |
||
| 2975 | * |
||
| 2976 | * @return array Array of invoices |
||
| 2977 | */ |
||
| 2978 | public function getInvoiceArrayList() |
||
| 2979 | { |
||
| 2980 | return $this->InvoiceArrayList($this->id); |
||
| 2981 | } |
||
| 2982 | |||
| 2983 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 2984 | /** |
||
| 2985 | * Returns an array with id and ref of related invoices |
||
| 2986 | * |
||
| 2987 | * @param int $id Id propal |
||
| 2988 | * @return array|int Array of invoices id |
||
| 2989 | */ |
||
| 2990 | public function InvoiceArrayList($id) |
||
| 2991 | { |
||
| 2992 | // phpcs:enable |
||
| 2993 | $ga = array(); |
||
| 2994 | $linkedInvoices = array(); |
||
| 2995 | |||
| 2996 | $this->fetchObjectLinked($id, $this->element); |
||
| 2997 | foreach ($this->linkedObjectsIds as $objecttype => $objectid) { |
||
| 2998 | // Nouveau système du comon object renvoi des rowid et non un id linéaire de 1 à n |
||
| 2999 | // On parcourt donc une liste d'objets en tant qu'objet unique |
||
| 3000 | foreach ($objectid as $key => $object) { |
||
| 3001 | // Cas des factures liees directement |
||
| 3002 | if ($objecttype == 'facture') { |
||
| 3003 | $linkedInvoices[] = $object; |
||
| 3004 | } else { |
||
| 3005 | // Cas des factures liees par un autre objet (ex: commande) |
||
| 3006 | $this->fetchObjectLinked($object, $objecttype); |
||
| 3007 | foreach ($this->linkedObjectsIds as $subobjecttype => $subobjectid) { |
||
| 3008 | foreach ($subobjectid as $subkey => $subobject) { |
||
| 3009 | if ($subobjecttype == 'facture') { |
||
| 3010 | $linkedInvoices[] = $subobject; |
||
| 3011 | } |
||
| 3012 | } |
||
| 3013 | } |
||
| 3014 | } |
||
| 3015 | } |
||
| 3016 | } |
||
| 3017 | |||
| 3018 | if (count($linkedInvoices) > 0) { |
||
| 3019 | $sql = "SELECT rowid as facid, ref, total_ht as total, datef as df, fk_user_author, fk_statut, paye"; |
||
| 3020 | $sql .= " FROM ".MAIN_DB_PREFIX."facture"; |
||
| 3021 | $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $linkedInvoices)).")"; |
||
| 3022 | |||
| 3023 | dol_syslog(get_class($this)."::InvoiceArrayList", LOG_DEBUG); |
||
| 3024 | $resql = $this->db->query($sql); |
||
| 3025 | |||
| 3026 | if ($resql) { |
||
| 3027 | $tab_sqlobj = array(); |
||
| 3028 | $nump = $this->db->num_rows($resql); |
||
| 3029 | for ($i = 0; $i < $nump; $i++) { |
||
| 3030 | $sqlobj = $this->db->fetch_object($resql); |
||
| 3031 | $tab_sqlobj[] = $sqlobj; |
||
| 3032 | } |
||
| 3033 | $this->db->free($resql); |
||
| 3034 | |||
| 3035 | $nump = count($tab_sqlobj); |
||
| 3036 | |||
| 3037 | if ($nump) { |
||
| 3038 | $i = 0; |
||
| 3039 | while ($i < $nump) { |
||
| 3040 | $obj = array_shift($tab_sqlobj); |
||
| 3041 | |||
| 3042 | $ga[$i] = $obj; |
||
| 3043 | |||
| 3044 | $i++; |
||
| 3045 | } |
||
| 3046 | } |
||
| 3047 | return $ga; |
||
| 3048 | } else { |
||
| 3049 | return -1; |
||
| 3050 | } |
||
| 3051 | } else { |
||
| 3052 | return $ga; |
||
| 3053 | } |
||
| 3054 | } |
||
| 3055 | |||
| 3056 | /** |
||
| 3057 | * Delete proposal |
||
| 3058 | * |
||
| 3059 | * @param User $user Object user that delete |
||
| 3060 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 3061 | * @return int >0 if OK, <=0 if KO |
||
| 3062 | */ |
||
| 3063 | public function delete($user, $notrigger = 0) |
||
| 3064 | { |
||
| 3065 | global $conf; |
||
| 3066 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 3067 | |||
| 3068 | $error = 0; |
||
| 3069 | |||
| 3070 | $this->db->begin(); |
||
| 3071 | |||
| 3072 | if (!$notrigger) { |
||
| 3073 | // Call trigger |
||
| 3074 | $result = $this->call_trigger('PROPAL_DELETE', $user); |
||
| 3075 | if ($result < 0) { |
||
| 3076 | $error++; |
||
| 3077 | } |
||
| 3078 | // End call triggers |
||
| 3079 | } |
||
| 3080 | |||
| 3081 | // Delete extrafields of lines and lines |
||
| 3082 | if (!$error && !empty($this->table_element_line)) { |
||
| 3083 | $tabletodelete = $this->table_element_line; |
||
| 3084 | $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).")"; |
||
| 3085 | $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id); |
||
| 3086 | if (!$this->db->query($sqlef) || !$this->db->query($sql)) { |
||
| 3087 | $error++; |
||
| 3088 | $this->error = $this->db->lasterror(); |
||
| 3089 | $this->errors[] = $this->error; |
||
| 3090 | dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); |
||
| 3091 | } |
||
| 3092 | } |
||
| 3093 | |||
| 3094 | if (!$error) { |
||
| 3095 | // Delete linked object |
||
| 3096 | $res = $this->deleteObjectLinked(); |
||
| 3097 | if ($res < 0) { |
||
| 3098 | $error++; |
||
| 3099 | } |
||
| 3100 | } |
||
| 3101 | |||
| 3102 | if (!$error) { |
||
| 3103 | // Delete linked contacts |
||
| 3104 | $res = $this->delete_linked_contact(); |
||
| 3105 | if ($res < 0) { |
||
| 3106 | $error++; |
||
| 3107 | } |
||
| 3108 | } |
||
| 3109 | |||
| 3110 | // Removed extrafields of object |
||
| 3111 | if (!$error) { |
||
| 3112 | $result = $this->deleteExtraFields(); |
||
| 3113 | if ($result < 0) { |
||
| 3114 | $error++; |
||
| 3115 | dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); |
||
| 3116 | } |
||
| 3117 | } |
||
| 3118 | |||
| 3119 | // Delete main record |
||
| 3120 | if (!$error) { |
||
| 3121 | $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id); |
||
| 3122 | $res = $this->db->query($sql); |
||
| 3123 | if (!$res) { |
||
| 3124 | $error++; |
||
| 3125 | $this->error = $this->db->lasterror(); |
||
| 3126 | $this->errors[] = $this->error; |
||
| 3127 | dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); |
||
| 3128 | } |
||
| 3129 | } |
||
| 3130 | |||
| 3131 | // Delete record into ECM index and physically |
||
| 3132 | if (!$error) { |
||
| 3133 | $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive |
||
| 3134 | if (!$res) { |
||
| 3135 | $error++; |
||
| 3136 | } |
||
| 3137 | } |
||
| 3138 | |||
| 3139 | if (!$error) { |
||
| 3140 | // We remove directory |
||
| 3141 | $ref = dol_sanitizeFileName($this->ref); |
||
| 3142 | if ($conf->propal->multidir_output[$this->entity] && !empty($this->ref)) { |
||
| 3143 | $dir = $conf->propal->multidir_output[$this->entity]."/".$ref; |
||
| 3144 | $file = $dir."/".$ref.".pdf"; |
||
| 3145 | if (file_exists($file)) { |
||
| 3146 | dol_delete_preview($this); |
||
| 3147 | |||
| 3148 | if (!dol_delete_file($file, 0, 0, 0, $this)) { |
||
| 3149 | $this->error = 'ErrorFailToDeleteFile'; |
||
| 3150 | $this->errors[] = $this->error; |
||
| 3151 | $this->db->rollback(); |
||
| 3152 | return 0; |
||
| 3153 | } |
||
| 3154 | } |
||
| 3155 | if (file_exists($dir)) { |
||
| 3156 | $res = @dol_delete_dir_recursive($dir); |
||
| 3157 | if (!$res) { |
||
| 3158 | $this->error = 'ErrorFailToDeleteDir'; |
||
| 3159 | $this->errors[] = $this->error; |
||
| 3160 | $this->db->rollback(); |
||
| 3161 | return 0; |
||
| 3162 | } |
||
| 3163 | } |
||
| 3164 | } |
||
| 3165 | } |
||
| 3166 | |||
| 3167 | if (!$error) { |
||
| 3168 | dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG); |
||
| 3169 | $this->db->commit(); |
||
| 3170 | return 1; |
||
| 3171 | } else { |
||
| 3172 | $this->db->rollback(); |
||
| 3173 | return -1; |
||
| 3174 | } |
||
| 3175 | } |
||
| 3176 | |||
| 3177 | /** |
||
| 3178 | * Change the delivery time |
||
| 3179 | * |
||
| 3180 | * @param int $availability_id Id of new delivery time |
||
| 3181 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 3182 | * @return int >0 if OK, <0 if KO |
||
| 3183 | * @deprecated use set_availability |
||
| 3184 | */ |
||
| 3185 | public function availability($availability_id, $notrigger = 0) |
||
| 3186 | { |
||
| 3187 | global $user; |
||
| 3188 | |||
| 3189 | if ($this->statut >= self::STATUS_DRAFT) { |
||
| 3190 | $error = 0; |
||
| 3191 | |||
| 3192 | $this->db->begin(); |
||
| 3193 | |||
| 3194 | $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal'; |
||
| 3195 | $sql .= ' SET fk_availability = '.((int) $availability_id); |
||
| 3196 | $sql .= ' WHERE rowid='.((int) $this->id); |
||
| 3197 | |||
| 3198 | dol_syslog(__METHOD__.' availability('.$availability_id.')', LOG_DEBUG); |
||
| 3199 | $resql = $this->db->query($sql); |
||
| 3200 | if (!$resql) { |
||
| 3201 | $this->errors[] = $this->db->error(); |
||
| 3202 | $error++; |
||
| 3203 | } |
||
| 3204 | |||
| 3205 | if (!$error) { |
||
| 3206 | $this->oldcopy = clone $this; |
||
| 3207 | $this->availability_id = $availability_id; |
||
| 3208 | } |
||
| 3209 | |||
| 3210 | if (!$notrigger && empty($error)) { |
||
| 3211 | // Call trigger |
||
| 3212 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 3213 | if ($result < 0) { |
||
| 3214 | $error++; |
||
| 3215 | } |
||
| 3216 | // End call triggers |
||
| 3217 | } |
||
| 3218 | |||
| 3219 | if (!$error) { |
||
| 3220 | $this->db->commit(); |
||
| 3221 | return 1; |
||
| 3222 | } else { |
||
| 3223 | foreach ($this->errors as $errmsg) { |
||
| 3224 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 3225 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 3226 | } |
||
| 3227 | $this->db->rollback(); |
||
| 3228 | return -1 * $error; |
||
| 3229 | } |
||
| 3230 | } else { |
||
| 3231 | $error_str = 'Propal status do not meet requirement '.$this->statut; |
||
| 3232 | dol_syslog(__METHOD__.$error_str, LOG_ERR); |
||
| 3233 | $this->error = $error_str; |
||
| 3234 | $this->errors[] = $this->error; |
||
| 3235 | return -2; |
||
| 3236 | } |
||
| 3237 | } |
||
| 3238 | |||
| 3239 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 3240 | /** |
||
| 3241 | * Change source demand |
||
| 3242 | * |
||
| 3243 | * @param int $demand_reason_id Id of new source demand |
||
| 3244 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 3245 | * @return int >0 si ok, <0 si ko |
||
| 3246 | * @deprecated use set_demand_reason |
||
| 3247 | */ |
||
| 3248 | public function demand_reason($demand_reason_id, $notrigger = 0) |
||
| 3249 | { |
||
| 3250 | // phpcs:enable |
||
| 3251 | global $user; |
||
| 3252 | |||
| 3253 | if ($this->statut >= self::STATUS_DRAFT) { |
||
| 3254 | $error = 0; |
||
| 3255 | |||
| 3256 | $this->db->begin(); |
||
| 3257 | |||
| 3258 | $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal'; |
||
| 3259 | $sql .= ' SET fk_input_reason = '.((int) $demand_reason_id); |
||
| 3260 | $sql .= ' WHERE rowid='.((int) $this->id); |
||
| 3261 | |||
| 3262 | dol_syslog(__METHOD__.' demand_reason('.$demand_reason_id.')', LOG_DEBUG); |
||
| 3263 | $resql = $this->db->query($sql); |
||
| 3264 | if (!$resql) { |
||
| 3265 | $this->errors[] = $this->db->error(); |
||
| 3266 | $error++; |
||
| 3267 | } |
||
| 3268 | |||
| 3269 | if (!$error) { |
||
| 3270 | $this->oldcopy = clone $this; |
||
| 3271 | $this->demand_reason_id = $demand_reason_id; |
||
| 3272 | } |
||
| 3273 | |||
| 3274 | if (!$notrigger && empty($error)) { |
||
| 3275 | // Call trigger |
||
| 3276 | $result = $this->call_trigger('PROPAL_MODIFY', $user); |
||
| 3277 | if ($result < 0) { |
||
| 3278 | $error++; |
||
| 3279 | } |
||
| 3280 | // End call triggers |
||
| 3281 | } |
||
| 3282 | |||
| 3283 | if (!$error) { |
||
| 3284 | $this->db->commit(); |
||
| 3285 | return 1; |
||
| 3286 | } else { |
||
| 3287 | foreach ($this->errors as $errmsg) { |
||
| 3288 | dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR); |
||
| 3289 | $this->error .= ($this->error ? ', '.$errmsg : $errmsg); |
||
| 3290 | } |
||
| 3291 | $this->db->rollback(); |
||
| 3292 | return -1 * $error; |
||
| 3293 | } |
||
| 3294 | } else { |
||
| 3295 | $error_str = 'Propal status do not meet requirement '.$this->statut; |
||
| 3296 | dol_syslog(__METHOD__.$error_str, LOG_ERR); |
||
| 3297 | $this->error = $error_str; |
||
| 3298 | $this->errors[] = $this->error; |
||
| 3299 | return -2; |
||
| 3300 | } |
||
| 3301 | } |
||
| 3302 | |||
| 3303 | |||
| 3304 | /** |
||
| 3305 | * Object Proposal Information |
||
| 3306 | * |
||
| 3307 | * @param int $id Proposal id |
||
| 3308 | * @return void |
||
| 3309 | */ |
||
| 3310 | public function info($id) |
||
| 3311 | { |
||
| 3312 | $sql = "SELECT c.rowid, "; |
||
| 3313 | $sql .= " c.datec, c.date_valid as datev, c.date_signature, c.date_cloture,"; |
||
| 3314 | $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_signature, c.fk_user_cloture"; |
||
| 3315 | $sql .= " FROM ".MAIN_DB_PREFIX."propal as c"; |
||
| 3316 | $sql .= " WHERE c.rowid = ".((int) $id); |
||
| 3317 | |||
| 3318 | $result = $this->db->query($sql); |
||
| 3319 | |||
| 3320 | if ($result) { |
||
| 3321 | if ($this->db->num_rows($result)) { |
||
| 3322 | $obj = $this->db->fetch_object($result); |
||
| 3323 | |||
| 3324 | $this->id = $obj->rowid; |
||
| 3325 | |||
| 3326 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 3327 | $this->date_validation = $this->db->jdate($obj->datev); |
||
| 3328 | $this->date_signature = $this->db->jdate($obj->date_signature); |
||
| 3329 | $this->date_cloture = $this->db->jdate($obj->date_cloture); |
||
| 3330 | |||
| 3331 | $cuser = new User($this->db); |
||
| 3332 | $cuser->fetch($obj->fk_user_author); |
||
| 3333 | $this->user_creation = $cuser; |
||
| 3334 | |||
| 3335 | if ($obj->fk_user_valid) { |
||
| 3336 | $vuser = new User($this->db); |
||
| 3337 | $vuser->fetch($obj->fk_user_valid); |
||
| 3338 | $this->user_validation = $vuser; |
||
| 3339 | } |
||
| 3340 | |||
| 3341 | if ($obj->fk_user_signature) { |
||
| 3342 | $user_signature = new User($this->db); |
||
| 3343 | $user_signature->fetch($obj->fk_user_signature); |
||
| 3344 | $this->user_signature = $user_signature; |
||
| 3345 | } |
||
| 3346 | |||
| 3347 | if ($obj->fk_user_cloture) { |
||
| 3348 | $cluser = new User($this->db); |
||
| 3349 | $cluser->fetch($obj->fk_user_cloture); |
||
| 3350 | $this->user_cloture = $cluser; |
||
| 3351 | } |
||
| 3352 | } |
||
| 3353 | $this->db->free($result); |
||
| 3354 | } else { |
||
| 3355 | dol_print_error($this->db); |
||
| 3356 | } |
||
| 3357 | } |
||
| 3358 | |||
| 3359 | |||
| 3360 | /** |
||
| 3361 | * Return label of status of proposal (draft, validated, ...) |
||
| 3362 | * |
||
| 3363 | * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 3364 | * @return string Label |
||
| 3365 | */ |
||
| 3366 | public function getLibStatut($mode = 0) |
||
| 3367 | { |
||
| 3368 | return $this->LibStatut($this->statut, $mode); |
||
| 3369 | } |
||
| 3370 | |||
| 3371 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 3372 | /** |
||
| 3373 | * Return label of a status (draft, validated, ...) |
||
| 3374 | * |
||
| 3375 | * @param int $status Id status |
||
| 3376 | * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 3377 | * @return string Label |
||
| 3378 | */ |
||
| 3379 | public function LibStatut($status, $mode = 1) |
||
| 3380 | { |
||
| 3381 | // phpcs:enable |
||
| 3382 | global $conf, $hookmanager; |
||
| 3383 | |||
| 3384 | // Init/load array of translation of status |
||
| 3385 | if (empty($this->labelStatus) || empty($this->labelStatusShort)) { |
||
| 3386 | global $langs; |
||
| 3387 | $langs->load("propal"); |
||
| 3388 | $this->labelStatus[0] = $langs->transnoentitiesnoconv("PropalStatusDraft"); |
||
| 3389 | $this->labelStatus[1] = $langs->transnoentitiesnoconv("PropalStatusValidated"); |
||
| 3390 | $this->labelStatus[2] = $langs->transnoentitiesnoconv("PropalStatusSigned"); |
||
| 3391 | $this->labelStatus[3] = $langs->transnoentitiesnoconv("PropalStatusNotSigned"); |
||
| 3392 | $this->labelStatus[4] = $langs->transnoentitiesnoconv("PropalStatusBilled"); |
||
| 3393 | $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("PropalStatusDraftShort"); |
||
| 3394 | $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("PropalStatusValidatedShort"); |
||
| 3395 | $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("PropalStatusSignedShort"); |
||
| 3396 | $this->labelStatusShort[3] = $langs->transnoentitiesnoconv("PropalStatusNotSignedShort"); |
||
| 3397 | $this->labelStatusShort[4] = $langs->transnoentitiesnoconv("PropalStatusBilledShort"); |
||
| 3398 | } |
||
| 3399 | |||
| 3400 | $statusType = ''; |
||
| 3401 | if ($status == self::STATUS_DRAFT) { |
||
| 3402 | $statusType = 'status0'; |
||
| 3403 | } elseif ($status == self::STATUS_VALIDATED) { |
||
| 3404 | $statusType = 'status1'; |
||
| 3405 | } elseif ($status == self::STATUS_SIGNED) { |
||
| 3406 | $statusType = 'status4'; |
||
| 3407 | } elseif ($status == self::STATUS_NOTSIGNED) { |
||
| 3408 | $statusType = 'status9'; |
||
| 3409 | } elseif ($status == self::STATUS_BILLED) { |
||
| 3410 | $statusType = 'status6'; |
||
| 3411 | } |
||
| 3412 | |||
| 3413 | |||
| 3414 | $parameters = array('status' => $status, 'mode' => $mode); |
||
| 3415 | $reshook = $hookmanager->executeHooks('LibStatut', $parameters, $this); // Note that $action and $object may have been modified by hook |
||
| 3416 | |||
| 3417 | if ($reshook > 0) { |
||
| 3418 | return $hookmanager->resPrint; |
||
| 3419 | } |
||
| 3420 | |||
| 3421 | return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode); |
||
| 3422 | } |
||
| 3423 | |||
| 3424 | |||
| 3425 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 3426 | /** |
||
| 3427 | * Load indicators for dashboard (this->nbtodo and this->nbtodolate) |
||
| 3428 | * |
||
| 3429 | * @param User $user Object user |
||
| 3430 | * @param int $mode "opened" for proposal to close, "signed" for proposal to invoice |
||
| 3431 | * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK |
||
| 3432 | */ |
||
| 3433 | public function load_board($user, $mode) |
||
| 3434 | { |
||
| 3435 | // phpcs:enable |
||
| 3436 | global $conf, $langs; |
||
| 3437 | |||
| 3438 | $clause = " WHERE"; |
||
| 3439 | |||
| 3440 | $sql = "SELECT p.rowid, p.ref, p.datec as datec, p.fin_validite as datefin, p.total_ht"; |
||
| 3441 | $sql .= " FROM ".MAIN_DB_PREFIX."propal as p"; |
||
| 3442 | if (empty($user->rights->societe->client->voir) && !$user->socid) { |
||
| 3443 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc"; |
||
| 3444 | $sql .= " WHERE sc.fk_user = ".((int) $user->id); |
||
| 3445 | $clause = " AND"; |
||
| 3446 | } |
||
| 3447 | $sql .= $clause." p.entity IN (".getEntity('propal').")"; |
||
| 3448 | if ($mode == 'opened') { |
||
| 3449 | $sql .= " AND p.fk_statut = ".self::STATUS_VALIDATED; |
||
| 3450 | } |
||
| 3451 | if ($mode == 'signed') { |
||
| 3452 | $sql .= " AND p.fk_statut = ".self::STATUS_SIGNED; |
||
| 3453 | } |
||
| 3454 | if ($user->socid) { |
||
| 3455 | $sql .= " AND p.fk_soc = ".((int) $user->socid); |
||
| 3456 | } |
||
| 3457 | |||
| 3458 | $resql = $this->db->query($sql); |
||
| 3459 | if ($resql) { |
||
| 3460 | $langs->load("propal"); |
||
| 3461 | $now = dol_now(); |
||
| 3462 | |||
| 3463 | $delay_warning = 0; |
||
| 3464 | $status = 0; |
||
| 3465 | $label = $labelShort = ''; |
||
| 3466 | if ($mode == 'opened') { |
||
| 3467 | $delay_warning = $conf->propal->cloture->warning_delay; |
||
| 3468 | $status = self::STATUS_VALIDATED; |
||
| 3469 | $label = $langs->transnoentitiesnoconv("PropalsToClose"); |
||
| 3470 | $labelShort = $langs->transnoentitiesnoconv("ToAcceptRefuse"); |
||
| 3471 | } |
||
| 3472 | if ($mode == 'signed') { |
||
| 3473 | $delay_warning = $conf->propal->facturation->warning_delay; |
||
| 3474 | $status = self::STATUS_SIGNED; |
||
| 3475 | $label = $langs->trans("PropalsToBill"); // We set here bill but may be billed or ordered |
||
| 3476 | $labelShort = $langs->trans("ToBill"); |
||
| 3477 | } |
||
| 3478 | |||
| 3479 | $response = new WorkboardResponse(); |
||
| 3480 | $response->warning_delay = $delay_warning / 60 / 60 / 24; |
||
| 3481 | $response->label = $label; |
||
| 3482 | $response->labelShort = $labelShort; |
||
| 3483 | $response->url = DOL_URL_ROOT.'/comm/propal/list.php?search_status='.$status.'&mainmenu=commercial&leftmenu=propals'; |
||
| 3484 | $response->url_late = DOL_URL_ROOT.'/comm/propal/list.php?search_status='.$status.'&mainmenu=commercial&leftmenu=propals&sortfield=p.datep&sortorder=asc'; |
||
| 3485 | $response->img = img_object('', "propal"); |
||
| 3486 | |||
| 3487 | // This assignment in condition is not a bug. It allows walking the results. |
||
| 3488 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 3489 | $response->nbtodo++; |
||
| 3490 | $response->total += $obj->total_ht; |
||
| 3491 | |||
| 3492 | if ($mode == 'opened') { |
||
| 3493 | $datelimit = $this->db->jdate($obj->datefin); |
||
| 3494 | if ($datelimit < ($now - $delay_warning)) { |
||
| 3495 | $response->nbtodolate++; |
||
| 3496 | } |
||
| 3497 | } |
||
| 3498 | // TODO Definir regle des propales a facturer en retard |
||
| 3499 | // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++; |
||
| 3500 | } |
||
| 3501 | |||
| 3502 | return $response; |
||
| 3503 | } else { |
||
| 3504 | $this->error = $this->db->error(); |
||
| 3505 | return -1; |
||
| 3506 | } |
||
| 3507 | } |
||
| 3508 | |||
| 3509 | |||
| 3510 | /** |
||
| 3511 | * Initialise an instance with random values. |
||
| 3512 | * Used to build previews or test instances. |
||
| 3513 | * id must be 0 if object instance is a specimen. |
||
| 3514 | * |
||
| 3515 | * @return void |
||
| 3516 | */ |
||
| 3517 | public function initAsSpecimen() |
||
| 3518 | { |
||
| 3519 | global $conf, $langs; |
||
| 3520 | |||
| 3521 | // Load array of products prodids |
||
| 3522 | $num_prods = 0; |
||
| 3523 | $prodids = array(); |
||
| 3524 | $sql = "SELECT rowid"; |
||
| 3525 | $sql .= " FROM ".MAIN_DB_PREFIX."product"; |
||
| 3526 | $sql .= " WHERE entity IN (".getEntity('product').")"; |
||
| 3527 | $sql .= $this->db->plimit(100); |
||
| 3528 | |||
| 3529 | $resql = $this->db->query($sql); |
||
| 3530 | if ($resql) { |
||
| 3531 | $num_prods = $this->db->num_rows($resql); |
||
| 3532 | $i = 0; |
||
| 3533 | while ($i < $num_prods) { |
||
| 3534 | $i++; |
||
| 3535 | $row = $this->db->fetch_row($resql); |
||
| 3536 | $prodids[$i] = $row[0]; |
||
| 3537 | } |
||
| 3538 | } |
||
| 3539 | |||
| 3540 | // Initialise parametres |
||
| 3541 | $this->id = 0; |
||
| 3542 | $this->ref = 'SPECIMEN'; |
||
| 3543 | $this->ref_client = 'NEMICEPS'; |
||
| 3544 | $this->specimen = 1; |
||
| 3545 | $this->socid = 1; |
||
| 3546 | $this->date = time(); |
||
| 3547 | $this->fin_validite = $this->date + 3600 * 24 * 30; |
||
| 3548 | $this->cond_reglement_id = 1; |
||
| 3549 | $this->cond_reglement_code = 'RECEP'; |
||
| 3550 | $this->mode_reglement_id = 7; |
||
| 3551 | $this->mode_reglement_code = 'CHQ'; |
||
| 3552 | $this->availability_id = 1; |
||
| 3553 | $this->availability_code = 'AV_NOW'; |
||
| 3554 | $this->demand_reason_id = 1; |
||
| 3555 | $this->demand_reason_code = 'SRC_00'; |
||
| 3556 | $this->note_public = 'This is a comment (public)'; |
||
| 3557 | $this->note_private = 'This is a comment (private)'; |
||
| 3558 | |||
| 3559 | $this->multicurrency_tx = 1; |
||
| 3560 | $this->multicurrency_code = $conf->currency; |
||
| 3561 | |||
| 3562 | // Lines |
||
| 3563 | $nbp = 5; |
||
| 3564 | $xnbp = 0; |
||
| 3565 | while ($xnbp < $nbp) { |
||
| 3566 | $line = new PropaleLigne($this->db); |
||
| 3567 | $line->desc = $langs->trans("Description")." ".$xnbp; |
||
| 3568 | $line->qty = 1; |
||
| 3569 | $line->subprice = 100; |
||
| 3570 | $line->price = 100; |
||
| 3571 | $line->tva_tx = 20; |
||
| 3572 | $line->localtax1_tx = 0; |
||
| 3573 | $line->localtax2_tx = 0; |
||
| 3574 | if ($xnbp == 2) { |
||
| 3575 | $line->total_ht = 50; |
||
| 3576 | $line->total_ttc = 60; |
||
| 3577 | $line->total_tva = 10; |
||
| 3578 | $line->remise_percent = 50; |
||
| 3579 | } else { |
||
| 3580 | $line->total_ht = 100; |
||
| 3581 | $line->total_ttc = 120; |
||
| 3582 | $line->total_tva = 20; |
||
| 3583 | $line->remise_percent = 00; |
||
| 3584 | } |
||
| 3585 | |||
| 3586 | if ($num_prods > 0) { |
||
| 3587 | $prodid = mt_rand(1, $num_prods); |
||
| 3588 | $line->fk_product = $prodids[$prodid]; |
||
| 3589 | $line->product_ref = 'SPECIMEN'; |
||
| 3590 | } |
||
| 3591 | |||
| 3592 | $this->lines[$xnbp] = $line; |
||
| 3593 | |||
| 3594 | $this->total_ht += $line->total_ht; |
||
| 3595 | $this->total_tva += $line->total_tva; |
||
| 3596 | $this->total_ttc += $line->total_ttc; |
||
| 3597 | |||
| 3598 | $xnbp++; |
||
| 3599 | } |
||
| 3600 | } |
||
| 3601 | |||
| 3602 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 3603 | /** |
||
| 3604 | * Charge indicateurs this->nb de tableau de bord |
||
| 3605 | * |
||
| 3606 | * @return int <0 if ko, >0 if ok |
||
| 3607 | */ |
||
| 3608 | public function load_state_board() |
||
| 3609 | { |
||
| 3610 | // phpcs:enable |
||
| 3611 | global $user; |
||
| 3612 | |||
| 3613 | $this->nb = array(); |
||
| 3614 | $clause = "WHERE"; |
||
| 3615 | |||
| 3616 | $sql = "SELECT count(p.rowid) as nb"; |
||
| 3617 | $sql .= " FROM ".MAIN_DB_PREFIX."propal as p"; |
||
| 3618 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid"; |
||
| 3619 | if (empty($user->rights->societe->client->voir) && !$user->socid) { |
||
| 3620 | $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc"; |
||
| 3621 | $sql .= " WHERE sc.fk_user = ".((int) $user->id); |
||
| 3622 | $clause = "AND"; |
||
| 3623 | } |
||
| 3624 | $sql .= " ".$clause." p.entity IN (".getEntity('propal').")"; |
||
| 3625 | |||
| 3626 | $resql = $this->db->query($sql); |
||
| 3627 | if ($resql) { |
||
| 3628 | // This assignment in condition is not a bug. It allows walking the results. |
||
| 3629 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 3630 | $this->nb["proposals"] = $obj->nb; |
||
| 3631 | } |
||
| 3632 | $this->db->free($resql); |
||
| 3633 | return 1; |
||
| 3634 | } else { |
||
| 3635 | dol_print_error($this->db); |
||
| 3636 | $this->error = $this->db->error(); |
||
| 3637 | return -1; |
||
| 3638 | } |
||
| 3639 | } |
||
| 3640 | |||
| 3641 | |||
| 3642 | /** |
||
| 3643 | * Returns the reference to the following non used Proposal used depending on the active numbering module |
||
| 3644 | * defined into PROPALE_ADDON |
||
| 3645 | * |
||
| 3646 | * @param Societe $soc Object thirdparty |
||
| 3647 | * @return string Reference libre pour la propale |
||
| 3648 | */ |
||
| 3649 | public function getNextNumRef($soc) |
||
| 3690 | } |
||
| 3691 | } |
||
| 3692 | |||
| 3693 | /** |
||
| 3694 | * getTooltipContentArray |
||
| 3695 | * @param array $params params to construct tooltip data |
||
| 3696 | * @since v18 |
||
| 3697 | * @return array |
||
| 3698 | */ |
||
| 3699 | public function getTooltipContentArray($params) |
||
| 3700 | { |
||
| 3701 | global $conf, $langs, $user; |
||
| 3702 | |||
| 3703 | $datas = []; |
||
| 3704 | |||
| 3705 | if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) { |
||
| 3706 | return ['optimize' => $langs->trans("Proposal")]; |
||
| 3707 | } |
||
| 3708 | if ($user->hasRight('propal', 'lire')) { |
||
| 3709 | $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Proposal").'</u>'; |
||
| 3710 | if (isset($this->statut)) { |
||
| 3711 | $datas['status'] = ' '.$this->getLibStatut(5); |
||
| 3712 | } |
||
| 3713 | if (!empty($this->ref)) { |
||
| 3714 | $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref; |
||
| 3715 | } |
||
| 3716 | if (!empty($this->ref_client)) { |
||
| 3717 | $datas['refcustomer'] = '<br><b>'.$langs->trans('RefCustomer').':</b> '.$this->ref_client; |
||
| 3718 | } |
||
| 3719 | if (!empty($this->total_ht)) { |
||
| 3720 | $datas['amountht'] = '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency); |
||
| 3721 | } |
||
| 3722 | if (!empty($this->total_tva)) { |
||
| 3723 | $datas['vat'] = '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency); |
||
| 3724 | } |
||
| 3725 | if (!empty($this->total_ttc)) { |
||
| 3726 | $datas['amountttc'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency); |
||
| 3727 | } |
||
| 3728 | if (!empty($this->date)) { |
||
| 3729 | $datas['date'] = '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day'); |
||
| 3730 | } |
||
| 3731 | if (!empty($this->delivery_date)) { |
||
| 3732 | $datas['deliverydate'] = '<br><b>'.$langs->trans('DeliveryDate').':</b> '.dol_print_date($this->delivery_date, 'dayhour'); |
||
| 3733 | } |
||
| 3734 | } |
||
| 3735 | |||
| 3736 | return $datas; |
||
| 3737 | } |
||
| 3738 | |||
| 3739 | /** |
||
| 3740 | * Return clicable link of object (with eventually picto) |
||
| 3741 | * |
||
| 3742 | * @param int $withpicto Add picto into link |
||
| 3743 | * @param string $option Where point the link ('expedition', 'document', ...) |
||
| 3744 | * @param string $get_params Parametres added to url |
||
| 3745 | * @param int $notooltip 1=Disable tooltip |
||
| 3746 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 3747 | * @param int $addlinktonotes -1=Disable, 0=Just add label show notes, 1=Add private note (only internal user), 2=Add public note (internal or external user), 3=Add private (internal user) and public note (internal and external user) |
||
| 3748 | * @return string String with URL |
||
| 3749 | */ |
||
| 3750 | public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = -1) |
||
| 3751 | { |
||
| 3752 | global $langs, $conf, $user, $hookmanager; |
||
| 3753 | |||
| 3754 | if (!empty($conf->dol_no_mouse_hover)) { |
||
| 3755 | $notooltip = 1; // Force disable tooltips |
||
| 3756 | } |
||
| 3757 | |||
| 3758 | $result = ''; |
||
| 3759 | $params = [ |
||
| 3760 | 'id' => $this->id, |
||
| 3761 | 'objecttype' => $this->element, |
||
| 3762 | 'option' => $option, |
||
| 3763 | ]; |
||
| 3764 | $classfortooltip = 'classfortooltip'; |
||
| 3765 | $dataparams = ''; |
||
| 3766 | if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { |
||
| 3767 | $classfortooltip = 'classforajaxtooltip'; |
||
| 3768 | $dataparams = ' data-params='.json_encode($params); |
||
| 3769 | // $label = $langs->trans('Loading'); |
||
| 3770 | } |
||
| 3771 | $label = implode($this->getTooltipContentArray($params)); |
||
| 3772 | |||
| 3773 | $url = ''; |
||
| 3774 | if ($user->rights->propal->lire) { |
||
| 3775 | if ($option == '') { |
||
| 3776 | $url = DOL_URL_ROOT.'/comm/propal/card.php?id='.$this->id.$get_params; |
||
| 3777 | } elseif ($option == 'compta') { // deprecated |
||
| 3778 | $url = DOL_URL_ROOT.'/comm/propal/card.php?id='.$this->id.$get_params; |
||
| 3779 | } elseif ($option == 'expedition') { |
||
| 3780 | $url = DOL_URL_ROOT.'/expedition/propal.php?id='.$this->id.$get_params; |
||
| 3781 | } elseif ($option == 'document') { |
||
| 3782 | $url = DOL_URL_ROOT.'/comm/propal/document.php?id='.$this->id.$get_params; |
||
| 3783 | } |
||
| 3784 | |||
| 3785 | if ($option != 'nolink') { |
||
| 3786 | // Add param to save lastsearch_values or not |
||
| 3787 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 3788 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 3789 | $add_save_lastsearch_values = 1; |
||
| 3790 | } |
||
| 3791 | if ($add_save_lastsearch_values) { |
||
| 3792 | $url .= '&save_lastsearch_values=1'; |
||
| 3793 | } |
||
| 3794 | } |
||
| 3795 | } |
||
| 3796 | |||
| 3797 | $linkclose = ''; |
||
| 3798 | if (empty($notooltip) && $user->rights->propal->lire) { |
||
| 3799 | if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) { |
||
| 3800 | $label = $langs->trans("Proposal"); |
||
| 3801 | $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
| 3802 | } |
||
| 3803 | $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"'; |
||
| 3804 | $linkclose .= $dataparams.' class="'.$classfortooltip.'"'; |
||
| 3805 | } |
||
| 3806 | |||
| 3807 | $linkstart = '<a href="'.$url.'"'; |
||
| 3808 | $linkstart .= $linkclose.'>'; |
||
| 3809 | $linkend = '</a>'; |
||
| 3810 | |||
| 3811 | $result .= $linkstart; |
||
| 3812 | if ($withpicto) { |
||
| 3813 | $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1); |
||
| 3814 | } |
||
| 3815 | if ($withpicto != 2) { |
||
| 3816 | $result .= $this->ref; |
||
| 3817 | } |
||
| 3818 | $result .= $linkend; |
||
| 3819 | |||
| 3820 | if ($addlinktonotes >= 0) { |
||
| 3821 | $txttoshow = ''; |
||
| 3822 | |||
| 3823 | if ($addlinktonotes == 0) { |
||
| 3824 | if (!empty($this->note_private) || !empty($this->note_public)) { |
||
| 3825 | $txttoshow = $langs->trans('ViewPrivateNote'); |
||
| 3826 | } |
||
| 3827 | } elseif ($addlinktonotes == 1) { |
||
| 3828 | if (!empty($this->note_private)) { |
||
| 3829 | $txttoshow .= ($user->socid > 0 ? '' : dol_string_nohtmltag($this->note_private, 1)); |
||
| 3830 | } |
||
| 3831 | } elseif ($addlinktonotes == 2) { |
||
| 3832 | if (!empty($this->note_public)) { |
||
| 3833 | $txttoshow .= dol_string_nohtmltag($this->note_public, 1); |
||
| 3834 | } |
||
| 3835 | } elseif ($addlinktonotes == 3) { |
||
| 3836 | if ($user->socid > 0) { |
||
| 3837 | if (!empty($this->note_public)) { |
||
| 3838 | $txttoshow .= dol_string_nohtmltag($this->note_public, 1); |
||
| 3839 | } |
||
| 3840 | } else { |
||
| 3841 | if (!empty($this->note_public)) { |
||
| 3842 | $txttoshow .= dol_string_nohtmltag($this->note_public, 1); |
||
| 3843 | } |
||
| 3844 | if (!empty($this->note_private)) { |
||
| 3845 | if (!empty($txttoshow)) { |
||
| 3846 | $txttoshow .= '<br><br>'; |
||
| 3847 | } |
||
| 3848 | $txttoshow .= dol_string_nohtmltag($this->note_private, 1); |
||
| 3849 | } |
||
| 3850 | } |
||
| 3851 | } |
||
| 3852 | |||
| 3853 | if ($txttoshow) { |
||
| 3854 | $result .= ' <span class="note inline-block">'; |
||
| 3855 | $result .= '<a href="'.DOL_URL_ROOT.'/comm/propal/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($txttoshow).'">'; |
||
| 3856 | $result .= img_picto('', 'note'); |
||
| 3857 | $result .= '</a>'; |
||
| 3858 | $result .= '</span>'; |
||
| 3859 | } |
||
| 3860 | } |
||
| 3861 | |||
| 3862 | global $action; |
||
| 3863 | $hookmanager->initHooks(array($this->element . 'dao')); |
||
| 3864 | $parameters = array('id'=>$this->id, 'getnomurl' => &$result); |
||
| 3865 | $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 3866 | if ($reshook > 0) { |
||
| 3867 | $result = $hookmanager->resPrint; |
||
| 3868 | } else { |
||
| 3869 | $result .= $hookmanager->resPrint; |
||
| 3870 | } |
||
| 3871 | return $result; |
||
| 3872 | } |
||
| 3873 | |||
| 3874 | /** |
||
| 3875 | * Retrieve an array of proposal lines |
||
| 3876 | * @param string $filters Filter on other fields |
||
| 3877 | * |
||
| 3878 | * @return int >0 if OK, <0 if KO |
||
| 3879 | */ |
||
| 3880 | public function getLinesArray($filters = '') |
||
| 3881 | { |
||
| 3882 | return $this->fetch_lines(0, 0, $filters); |
||
| 3883 | } |
||
| 3884 | |||
| 3885 | /** |
||
| 3886 | * Create a document onto disk according to template module. |
||
| 3887 | * |
||
| 3888 | * @param string $modele Force model to use ('' to not force) |
||
| 3889 | * @param Translate $outputlangs Object langs to use for output |
||
| 3890 | * @param int $hidedetails Hide details of lines |
||
| 3891 | * @param int $hidedesc Hide description |
||
| 3892 | * @param int $hideref Hide ref |
||
| 3893 | * @param null|array $moreparams Array to provide more information |
||
| 3894 | * @return int 0 if KO, 1 if OK |
||
| 3895 | */ |
||
| 3896 | public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null) |
||
| 3916 | } |
||
| 3917 | |||
| 3918 | /** |
||
| 3919 | * Function used to replace a thirdparty id with another one. |
||
| 3920 | * |
||
| 3921 | * @param DoliDB $dbs Database handler, because function is static we name it $dbs not $db to avoid breaking coding test |
||
| 3922 | * @param int $origin_id Old thirdparty id |
||
| 3923 | * @param int $dest_id New thirdparty id |
||
| 3924 | * @return bool |
||
| 3925 | */ |
||
| 3926 | public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id) |
||
| 3927 | { |
||
| 3928 | $tables = array( |
||
| 3929 | 'propal' |
||
| 3930 | ); |
||
| 3931 | |||
| 3932 | return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables); |
||
| 3933 | } |
||
| 3934 | |||
| 3935 | /** |
||
| 3936 | * Function used to replace a product id with another one. |
||
| 3937 | * |
||
| 3938 | * @param DoliDB $db Database handler |
||
| 3939 | * @param int $origin_id Old product id |
||
| 3940 | * @param int $dest_id New product id |
||
| 3941 | * @return bool |
||
| 3942 | */ |
||
| 3943 | public static function replaceProduct(DoliDB $db, $origin_id, $dest_id) |
||
| 3944 | { |
||
| 3945 | $tables = array( |
||
| 3946 | 'propaldet' |
||
| 3947 | ); |
||
| 3948 | |||
| 3949 | return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables); |
||
| 3950 | } |
||
| 3951 | |||
| 3952 | /** |
||
| 3953 | * Return clicable link of object (with eventually picto) |
||
| 3954 | * |
||
| 3955 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
| 3956 | * @param array $arraydata Array of data |
||
| 3957 | * @return string HTML Code for Kanban thumb. |
||
| 3958 | */ |
||
| 3959 | public function getKanbanView($option = '', $arraydata = null) |
||
| 3986 | } |
||
| 3987 | } |
||
| 3988 | |||
| 3989 | /** |
||
| 3990 | * Class to manage commercial proposal lines |
||
| 3991 | */ |
||
| 4635 |