Skip to content

Commit 7e80696

Browse files
committed
FIX can clone a propale, order, invoice with an another entity
1 parent 2a5c29f commit 7e80696

File tree

6 files changed

+95
-71
lines changed

6 files changed

+95
-71
lines changed

htdocs/comm/propal/card.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@
187187
setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('IdThirdParty')), null, 'errors');
188188
} else {
189189
if ($object->id > 0) {
190+
$objectutil = dol_clone($object, 1); // We use a clone to avoid to denaturate loaded object when setting some properties for clone. We use native clone to keep this->db valid.
191+
'@phan-var-force Propal $objectutil';
192+
190193
if (getDolGlobalString('PROPAL_CLONE_DATE_DELIVERY')) {
191194
//Get difference between old and new delivery date and change lines according to difference
192195
$date_delivery = dol_mktime(
@@ -204,31 +207,34 @@
204207
12,
205208
0,
206209
0,
207-
dol_print_date($date_delivery_old, '%m'),
208-
dol_print_date($date_delivery_old, '%d'),
209-
dol_print_date($date_delivery_old, '%Y')
210+
(int) dol_print_date($date_delivery_old, '%m'),
211+
(int) dol_print_date($date_delivery_old, '%d'),
212+
(int) dol_print_date($date_delivery_old, '%Y')
210213
);
211214
//Calculate the difference and apply if necessary
212215
$difference = $date_delivery - $old_date_delivery;
213216
if ($difference != 0) {
214-
$object->delivery_date = $date_delivery;
215-
foreach ($object->lines as $line) {
217+
$objectutil->delivery_date = $date_delivery;
218+
foreach ($objectutil->lines as $line) {
216219
if (isset($line->date_start)) {
217-
$line->date_start = $line->date_start + $difference;
220+
$line->date_start += $difference;
218221
}
219222
if (isset($line->date_end)) {
220-
$line->date_end = $line->date_end + $difference;
223+
$line->date_end += $difference;
221224
}
222225
}
223226
}
224227
}
225228
}
226229

227-
$result = $object->createFromClone($user, $socid, (GETPOSTISSET('entity') ? GETPOSTINT('entity') : null), (GETPOST('update_prices') == 'on'), (GETPOST('update_desc') == 'on'));
230+
$objectutil->socid = $socid;
231+
$objectutil->entity = (GETPOSTISSET('entity') ? GETPOSTINT('entity') : $conf->entity);
232+
233+
$result = $objectutil->createFromClone($user, $socid, (GETPOSTISSET('entity') ? GETPOSTINT('entity') : null), (GETPOST('update_prices') == 'on'), (GETPOST('update_desc') == 'on'));
228234
if ($result > 0) {
229235
$warningMsgLineList = array();
230236
// check all product lines are to sell otherwise add a warning message for each product line is not to sell
231-
foreach ($object->lines as $line) {
237+
foreach ($objectutil->lines as $line) {
232238
if (!is_object($line->product)) {
233239
$line->fetch_product();
234240
}
@@ -245,8 +251,8 @@
245251
header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
246252
exit();
247253
} else {
248-
if (count($object->errors) > 0) {
249-
setEventMessages($object->error, $object->errors, 'errors');
254+
if (count($objectutil->errors) > 0) {
255+
setEventMessages($objectutil->error, $objectutil->errors, 'errors');
250256
}
251257
$action = '';
252258
}

htdocs/comm/propal/class/propal.class.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,15 +1359,15 @@ public function create($user, $notrigger = 0)
13591359
* Load an object from its id and create a new one in database
13601360
*
13611361
* @param User $user User making the clone
1362-
* @param int $socid Id of thirdparty
1363-
* @param int $forceentity Entity id to force
1362+
* @param int $socid Id of thirdparty (deprecated?)
1363+
* @param int $forceentity Entity id to force (deprecated?)
13641364
* @param bool $update_prices [=false] Update prices if true
13651365
* @param bool $update_desc [=false] Update description if true
13661366
* @return int New id of clone
13671367
*/
13681368
public function createFromClone(User $user, $socid = 0, $forceentity = null, $update_prices = false, $update_desc = false)
13691369
{
1370-
global $conf, $hookmanager, $mysoc;
1370+
global $hookmanager, $mysoc;
13711371

13721372
dol_include_once('/projet/class/project.class.php');
13731373

@@ -1382,12 +1382,18 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
13821382

13831383
// Load source object
13841384
$object->fetch($this->id);
1385+
$object->entity = (!empty($forceentity) ? $forceentity : $this->entity); // for compatibility
1386+
1387+
// Load source object
1388+
$objFrom = clone $object;
13851389

13861390
$objsoc = new Societe($this->db);
13871391

1392+
$this->socid = (!empty($socid) ? $socid : $this->socid); // for compatibility
1393+
13881394
// Change socid if needed
1389-
if (!empty($socid) && $socid != $object->socid) {
1390-
if ($objsoc->fetch($socid) > 0) {
1395+
if (!empty($this->socid) && $this->socid != $object->socid) {
1396+
if ($objsoc->fetch($this->socid) > 0) {
13911397
$object->socid = $objsoc->id;
13921398
$object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
13931399
$object->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : null);
@@ -1410,6 +1416,7 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
14101416
// reset ref_client
14111417
if (!getDolGlobalString('MAIN_KEEP_REF_CUSTOMER_ON_CLONING')) {
14121418
$object->ref_client = '';
1419+
$object->ref_customer = '';
14131420
}
14141421

14151422
// TODO Change product price if multi-prices
@@ -1434,14 +1441,14 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
14341441
if ($res > 0) {
14351442
if ($update_prices === true) {
14361443
$pu_ht = $prod->price;
1437-
$tva_tx = get_default_tva($mysoc, $objsoc, $prod->id);
1444+
$tva_tx = (string) get_default_tva($mysoc, $objsoc, $prod->id);
14381445
$remise_percent = $objsoc->remise_percent;
14391446

14401447
if (getDolGlobalString('PRODUIT_MULTIPRICES') && $objsoc->price_level > 0) {
14411448
$pu_ht = $prod->multiprices[$objsoc->price_level];
14421449
if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) { // using this option is a bug. kept for backward compatibility
14431450
if (isset($prod->multiprices_tva_tx[$objsoc->price_level])) {
1444-
$tva_tx = $prod->multiprices_tva_tx[$objsoc->price_level];
1451+
$tva_tx = (string) $prod->multiprices_tva_tx[$objsoc->price_level];
14451452
}
14461453
}
14471454
} elseif (getDolGlobalString('PRODUIT_CUSTOMER_PRICES')) {
@@ -1475,7 +1482,6 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
14751482

14761483
$object->id = 0;
14771484
$object->ref = '';
1478-
$object->entity = (!empty($forceentity) ? $forceentity : $object->entity);
14791485
$object->statut = self::STATUS_DRAFT;
14801486

14811487
// Clear fields
@@ -1486,6 +1492,7 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
14861492
$object->fin_validite = $object->date + ($object->duree_validite * 24 * 3600);
14871493
if (!getDolGlobalString('MAIN_KEEP_REF_CUSTOMER_ON_CLONING')) {
14881494
$object->ref_client = '';
1495+
$object->ref_customer = '';
14891496
}
14901497
if (getDolGlobalInt('MAIN_DONT_KEEP_NOTE_ON_CLONING') == 1) {
14911498
$object->note_private = '';
@@ -1502,15 +1509,15 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
15021509

15031510
if (!$error && !getDolGlobalInt('MAIN_IGNORE_CONTACTS_ON_CLONING')) {
15041511
// copy internal contacts
1505-
if ($object->copy_linked_contact($this, 'internal') < 0) {
1512+
if ($object->copy_linked_contact($objFrom, 'internal') < 0) {
15061513
$error++;
15071514
}
15081515
}
15091516

15101517
if (!$error) {
15111518
// copy external contacts if same company
1512-
if ($this->socid == $object->socid) {
1513-
if ($object->copy_linked_contact($this, 'external') < 0) {
1519+
if ($objFrom->socid == $object->socid) {
1520+
if ($object->copy_linked_contact($objFrom, 'external') < 0) {
15141521
$error++;
15151522
}
15161523
}
@@ -1519,7 +1526,7 @@ public function createFromClone(User $user, $socid = 0, $forceentity = null, $up
15191526
if (!$error) {
15201527
// Hook of thirdparty module
15211528
if (is_object($hookmanager)) {
1522-
$parameters = array('objFrom' => $this, 'clonedObj' => $object);
1529+
$parameters = array('objFrom' => $objFrom, 'clonedObj' => $object);
15231530
$action = '';
15241531
$reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
15251532
if ($reshook < 0) {

htdocs/commande/card.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,17 @@
192192
setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('IdThirdParty')), null, 'errors');
193193
} else {
194194
if ($object->id > 0) {
195-
// Because createFromClone modifies the object, we must clone it so that we can restore it later
196-
$orig = clone $object;
195+
$objectutil = dol_clone($object, 1); // We use a clone to avoid to denaturate loaded object when setting some properties for clone. We use native clone to keep this->db valid.
196+
'@phan-var-force Commande $objectutil';
197197

198-
$result = $object->createFromClone($user, $socid);
198+
$objectutil->socid = $socid;
199+
$objectutil->entity = (GETPOSTISSET('entity') ? GETPOSTINT('entity') : $conf->entity);
200+
201+
$result = $objectutil->createFromClone($user, $socid);
199202
if ($result > 0) {
200203
$warningMsgLineList = array();
201204
// check all product lines are to sell otherwise add a warning message for each product line is not to sell
202-
foreach ($object->lines as $line) {
205+
foreach ($objectutil->lines as $line) {
203206
if (!is_object($line->product)) {
204207
$line->fetch_product();
205208
}
@@ -216,8 +219,7 @@
216219
header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
217220
exit;
218221
} else {
219-
setEventMessages($object->error, $object->errors, 'errors');
220-
$object = $orig;
222+
setEventMessages($objectutil->error, $objectutil->errors, 'errors');
221223
$action = '';
222224
}
223225
}

htdocs/commande/class/commande.class.php

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,80 +1179,83 @@ public function create($user, $notrigger = 0)
11791179
* Load an object from its id and create a new one in database
11801180
*
11811181
* @param User $user User making the clone
1182-
* @param int $socid Id of thirdparty
1182+
* @param int $socid Id of thirdparty (deprecated?)
11831183
* @return int New id of clone
11841184
*/
11851185
public function createFromClone(User $user, $socid = 0)
11861186
{
1187-
global $conf, $user, $hookmanager;
1187+
global $user, $hookmanager;
11881188

11891189
$error = 0;
11901190

1191+
$object = new self($this->db);
1192+
11911193
$this->db->begin();
11921194

1193-
// get lines so they will be clone
1194-
foreach ($this->lines as $line) {
1195-
$line->fetch_optionals();
1196-
}
1195+
// Load source object
1196+
$object->fetch($this->id);
1197+
$object->entity = $this->entity;
11971198

11981199
// Load source object
1199-
$objFrom = clone $this;
1200+
$objFrom = clone $object;
1201+
1202+
$this->socid = (!empty($socid) ? $socid : $this->socid); // for compatibility
12001203

12011204
// Change socid if needed
1202-
if (!empty($socid) && $socid != $this->socid) {
1205+
if (!empty($this->socid) && $this->socid != $object->socid) {
12031206
$objsoc = new Societe($this->db);
12041207

1205-
if ($objsoc->fetch($socid) > 0) {
1206-
$this->socid = $objsoc->id;
1207-
$this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1208-
$this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : 0);
1209-
$this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1210-
$this->fk_project = 0;
1211-
$this->fk_delivery_address = 0;
1208+
if ($objsoc->fetch($this->socid) > 0) {
1209+
$object->socid = $objsoc->id;
1210+
$object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1211+
$object->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : 0);
1212+
$object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1213+
$object->fk_project = 0;
1214+
$object->fk_delivery_address = 0;
12121215
}
12131216

12141217
// TODO Change product price if multi-prices
12151218
}
12161219

1217-
$this->id = 0;
1218-
$this->ref = '';
1219-
$this->statut = self::STATUS_DRAFT;
1220+
$object->id = 0;
1221+
$object->ref = '';
1222+
$object->statut = self::STATUS_DRAFT;
12201223

12211224
// Clear fields
1222-
$this->user_author_id = $user->id;
1223-
$this->user_validation_id = 0;
1224-
$this->date = dol_now();
1225-
$this->date_commande = dol_now();
1226-
$this->date_creation = '';
1227-
$this->date_validation = '';
1225+
$object->user_author_id = $user->id;
1226+
$object->user_validation_id = 0;
1227+
$object->date = dol_now();
1228+
$object->date_commande = dol_now();
1229+
$object->date_creation = '';
1230+
$object->date_validation = '';
12281231
if (!getDolGlobalString('MAIN_KEEP_REF_CUSTOMER_ON_CLONING')) {
1229-
$this->ref_client = '';
1230-
$this->ref_customer = '';
1232+
$object->ref_client = '';
1233+
$object->ref_customer = '';
12311234
}
12321235

12331236
// Do not clone ref_ext
1234-
$num = count($this->lines);
1237+
$num = count($object->lines);
12351238
for ($i = 0; $i < $num; $i++) {
1236-
$this->lines[$i]->ref_ext = '';
1239+
$object->lines[$i]->ref_ext = '';
12371240
}
12381241

12391242
// Create clone
1240-
$this->context['createfromclone'] = 'createfromclone';
1241-
$result = $this->create($user);
1243+
$object->context['createfromclone'] = 'createfromclone';
1244+
$result = $object->create($user);
12421245
if ($result < 0) {
12431246
$error++;
12441247
}
12451248

12461249
if (!$error) {
12471250
// copy internal contacts
1248-
if ($this->copy_linked_contact($objFrom, 'internal') < 0) {
1251+
if ($object->copy_linked_contact($objFrom, 'internal') < 0) {
12491252
$error++;
12501253
}
12511254
}
12521255

12531256
if (!$error) {
12541257
// copy external contacts if same company
1255-
if ($this->socid == $objFrom->socid) {
1258+
if ($object->socid == $objFrom->socid) {
12561259
if ($this->copy_linked_contact($objFrom, 'external') < 0) {
12571260
$error++;
12581261
}
@@ -1262,22 +1265,22 @@ public function createFromClone(User $user, $socid = 0)
12621265
if (!$error) {
12631266
// Hook of thirdparty module
12641267
if (is_object($hookmanager)) {
1265-
$parameters = array('objFrom' => $objFrom);
1268+
$parameters = array('objFrom' => $objFrom, 'clonedObj' => $object);
12661269
$action = '';
1267-
$reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1270+
$reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
12681271
if ($reshook < 0) {
12691272
$this->setErrorsFromObject($hookmanager);
12701273
$error++;
12711274
}
12721275
}
12731276
}
12741277

1275-
unset($this->context['createfromclone']);
1278+
unset($object->context['createfromclone']);
12761279

12771280
// End
12781281
if (!$error) {
12791282
$this->db->commit();
1280-
return $this->id;
1283+
return $object->id;
12811284
} else {
12821285
$this->db->rollback();
12831286
return -1;

htdocs/compta/facture/card.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@
230230

231231
$objectutil->date = dol_mktime(12, 0, 0, GETPOSTINT('newdatemonth'), GETPOSTINT('newdateday'), GETPOSTINT('newdateyear'));
232232
$objectutil->socid = $socid;
233+
$objectutil->entity = (GETPOSTISSET('entity') ? GETPOSTINT('entity') : $conf->entity);
234+
233235
$result = $objectutil->createFromClone($user, $id);
234236
if ($result > 0) {
235237
$warningMsgLineList = array();

0 commit comments

Comments
 (0)