Contractqueue: commit to Contract finished
This commit is contained in:
@@ -83,19 +83,27 @@ class ContractqueueController extends mfBaseController {
|
||||
if(ContractqueueModel::getFirst(["order_id" => $order->id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$skip_order = false;
|
||||
$contracts = [];
|
||||
$primary_matchcode = false;
|
||||
|
||||
|
||||
|
||||
foreach($order->products as $op) {
|
||||
$contract = ContractModel::getFirst(["orderproduct_id" => $op->id]);
|
||||
if(!$contract) {
|
||||
$contract = ContractqueueModel::getFirst(["orderproduct_id" => $op->id]);
|
||||
}
|
||||
if($contract && $contract->matchcode) {
|
||||
$primary_matchcode = $contract->matchcode;
|
||||
continue;
|
||||
if($contract) {
|
||||
if($contract->matchcode) {
|
||||
$primary_matchcode = $contract->matchcode;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if($op->product->external) {
|
||||
$skip_order = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// if contract does not exist yet, create Contractqueue
|
||||
@@ -126,7 +134,11 @@ class ContractqueueController extends mfBaseController {
|
||||
$contract->crediting_matchcode = $order->owner->getCompanyOrName().", ".$order->owner->street.", ".$order->owner->zip." ".$order->owner->city;
|
||||
$contracts[] = $contract;
|
||||
}
|
||||
|
||||
|
||||
if($skip_order) {
|
||||
$contracts = [];
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!$primary_matchcode) {
|
||||
$primary_matchcode = $order->owner->street.", ".$order->owner->zip." ".$order->owner->city;
|
||||
@@ -176,22 +188,41 @@ class ContractqueueController extends mfBaseController {
|
||||
$new_contracts = [];
|
||||
$c = 0;
|
||||
$o = 0;
|
||||
$last_order_id = 0;
|
||||
|
||||
foreach(ContractqueueModel::search(["approved" => true, "contract_id" => null]) as $cq) {
|
||||
//var_dump($cq);exit;
|
||||
$contract = new Contract($cq->orderproduct_id);
|
||||
if($contract->id) continue; // contract should not yet exist
|
||||
$contract = ContractModel::getFirst(["orderproduct_id" => $cq->orderproduct_id]);
|
||||
if($contract) {
|
||||
$this->log->debug("Contract von orderproduct ".$cq->orderproduct_id." existiert schon: ".$contract->id);
|
||||
continue;
|
||||
} // contract should not yet exist
|
||||
|
||||
$order_id = $cq->orderproduct->order_id;
|
||||
|
||||
if($c >= 2000) {
|
||||
if($order_id != $last_order_id) {
|
||||
$this->layout()->setFlash("$c Contracts (exkl. Gutschriften) erstellt", "info");
|
||||
$this->redirect("Contractqueue");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$last_order_id = $order_id;
|
||||
|
||||
/*
|
||||
* Create Contract
|
||||
*/
|
||||
$contract = ContractModel::createFromContractqueue($cq);
|
||||
if(!$contract->save()) {
|
||||
$this->layout()->setFlash("Eine Position in Bestellung ".$cq->order_id." konnte nicht übernommen werden: Fehler beim Speichern");
|
||||
$this->log->debug("Eine Position in Bestellung ".$cq->order_id." konnte nicht übernommen werden: Fehler beim Speichern");
|
||||
$this->layout()->setFlash("Eine Position in Bestellung ".$cq->order_id." konnte nicht übernommen werden: Fehler beim Speichern", "warning");
|
||||
continue;
|
||||
}
|
||||
|
||||
$c++;
|
||||
|
||||
|
||||
$cq->contract_id = $contract->id;
|
||||
$cq->save();
|
||||
|
||||
@@ -213,7 +244,8 @@ class ContractqueueController extends mfBaseController {
|
||||
if($cq->approved_credit) {
|
||||
$credit = ContractModel::createFromContractQueue($cq, "credit");
|
||||
if(!$credit->save()) {
|
||||
$this->layout()->setFlash("Zu einer Position in Bestellung ".$cq->order_id." konnte keine Gutschrift erstellt werden: Fehler beim Speichern");
|
||||
$this->log->log("Zu einer Position in Bestellung ".$cq->order_id." konnte keine Gutschrift erstellt werden: Fehler beim Speichern");
|
||||
$this->layout()->setFlash("Zu einer Position in Bestellung ".$cq->order_id." konnte keine Gutschrift erstellt werden: Fehler beim Speichern", "warning");
|
||||
continue;
|
||||
}
|
||||
$journal = ContractjournalModel::create([
|
||||
@@ -229,8 +261,8 @@ class ContractqueueController extends mfBaseController {
|
||||
]);
|
||||
$journal->save();
|
||||
$link = ContractLinkModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'origin_contract_id' => $credit->id,
|
||||
'contract_id' => $credit->id,
|
||||
'origin_contract_id' => $contract->id,
|
||||
'type' => 'credit'
|
||||
]);
|
||||
$link->save();
|
||||
@@ -240,39 +272,43 @@ class ContractqueueController extends mfBaseController {
|
||||
/*
|
||||
* Create ContractLinks
|
||||
*/
|
||||
foreach($new_contracts as $origin) {
|
||||
if(ContractLinkModel::getFirst(["contract_id" => $contract->id, "origin_contract_id" => $origin->id])) {
|
||||
continue;
|
||||
}
|
||||
$link = ContractLinkModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'origin_contract_id' => $origin->id,
|
||||
'type' => 'link'
|
||||
]);
|
||||
$link->save();
|
||||
if($link->id) {
|
||||
$journal = ContractjournalModel::create([
|
||||
if(array_key_exists($order_id, $new_contracts)) {
|
||||
foreach ($new_contracts[$order_id] as $origin) {
|
||||
if (ContractLinkModel::getFirst(["contract_id" => $contract->id, "origin_contract_id" => $origin->id])) {
|
||||
continue;
|
||||
}
|
||||
$link = ContractLinkModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'type' => "link",
|
||||
'value' => $origin->id
|
||||
'origin_contract_id' => $origin->id,
|
||||
'type' => 'link'
|
||||
]);
|
||||
$journal->save();
|
||||
$link->save();
|
||||
if ($link->id) {
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'type' => "link",
|
||||
'value' => $origin->id
|
||||
]);
|
||||
$journal->save();
|
||||
|
||||
$ojournal = ContractjournalModel::create([
|
||||
'contract_id' => $origin->id,
|
||||
'type' => "link",
|
||||
'value' => $contract->id
|
||||
]);
|
||||
$ojournal->save();
|
||||
$ojournal = ContractjournalModel::create([
|
||||
'contract_id' => $origin->id,
|
||||
'type' => "link",
|
||||
'value' => $contract->id
|
||||
]);
|
||||
$ojournal->save();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$new_contracts[$order_id] = [];
|
||||
}
|
||||
|
||||
$new_contracts[] = $contract;
|
||||
$new_contracts[$order_id][] = $contract;
|
||||
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("$c Contracts erstellt", "success");
|
||||
$this->redirect("Contract");
|
||||
$this->redirect("Contractqueue");
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user