Contract WIP & Contractqueue WIP 2024-04-18
This commit is contained in:
@@ -167,6 +167,114 @@ class ContractqueueController extends mfBaseController {
|
||||
}
|
||||
$this->layout()->setFlash("Alle Bestellungen importiert", "success");
|
||||
$this->redirect("Contractqueue");
|
||||
}
|
||||
|
||||
protected function commitAction() {
|
||||
$r = $this->request;
|
||||
//var_dump($r->get());exit;
|
||||
|
||||
$new_contracts = [];
|
||||
$c = 0;
|
||||
$o = 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
|
||||
|
||||
/*
|
||||
* 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");
|
||||
continue;
|
||||
}
|
||||
|
||||
$c++;
|
||||
|
||||
$cq->contract_id = $contract->id;
|
||||
$cq->save();
|
||||
|
||||
// create Contractjournal
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'type' => "created_from",
|
||||
'value' => "order"
|
||||
]);
|
||||
$journal->save();
|
||||
|
||||
$contract->addFilesFromOrder();
|
||||
|
||||
/*
|
||||
* Create Crediting Contract
|
||||
* if required
|
||||
*/
|
||||
|
||||
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");
|
||||
continue;
|
||||
}
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $credit->id,
|
||||
'type' => "created_from",
|
||||
'value' => "order"
|
||||
]);
|
||||
$journal->save();
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'type' => "credit_created",
|
||||
'value' => $credit->id
|
||||
]);
|
||||
$journal->save();
|
||||
$link = ContractLinkModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
'origin_contract_id' => $credit->id,
|
||||
'type' => 'credit'
|
||||
]);
|
||||
$link->save();
|
||||
// XXX: retour link erstellen?
|
||||
}
|
||||
|
||||
/*
|
||||
* 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([
|
||||
'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();
|
||||
}
|
||||
}
|
||||
|
||||
$new_contracts[] = $contract;
|
||||
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("$c Contracts erstellt", "success");
|
||||
$this->redirect("Contract");
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function apiAction() {
|
||||
@@ -180,6 +288,9 @@ class ContractqueueController extends mfBaseController {
|
||||
case "saveContract":
|
||||
$return = $this->saveContractApi();
|
||||
break;
|
||||
case "setApproval":
|
||||
$return = $this->setApprovalApi();
|
||||
break;
|
||||
default:
|
||||
$return = false;
|
||||
}
|
||||
@@ -304,4 +415,31 @@ class ContractqueueController extends mfBaseController {
|
||||
return ["contract" => $return, "credit" => $credit];
|
||||
|
||||
}
|
||||
|
||||
private function setApprovalApi() {
|
||||
$r = $this->request;
|
||||
|
||||
//var_dump($r->get());exit;
|
||||
|
||||
$id = $r->cq_id;
|
||||
if(!is_numeric($id) || $id < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cq = new Contractqueue($id);
|
||||
if(!$cq->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if($r->type == "order") {
|
||||
$cq->approved = $r->value;
|
||||
}
|
||||
if($r->type == "credit") {
|
||||
$cq->approved_credit = $r->value;
|
||||
}
|
||||
|
||||
$cq->save();
|
||||
|
||||
return ["message" => "contract saved"];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user