Contract WIP & Contractqueue WIP 2024-04-18
This commit is contained in:
@@ -50,6 +50,9 @@ class Contract extends mfBaseModel {
|
||||
case "link":
|
||||
$this->linkTo[] = $link;
|
||||
break;
|
||||
case "credit":
|
||||
$this->linkFrom[] = $link;
|
||||
break;
|
||||
case "upgrade":
|
||||
$this->upgradeTo[] = $link;
|
||||
break;
|
||||
@@ -73,6 +76,9 @@ class Contract extends mfBaseModel {
|
||||
case "link":
|
||||
$this->linkFrom[] = $link;
|
||||
break;
|
||||
case "credit":
|
||||
$this->linkTo[] = $link;
|
||||
break;
|
||||
case "upgrade":
|
||||
$this->upgradeFrom[] = $link;
|
||||
break;
|
||||
@@ -126,7 +132,40 @@ class Contract extends mfBaseModel {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function addFilesFromOrder() {
|
||||
if(!$this->orderproduct_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$op = $this->getProperty("orderproduct");
|
||||
$order = $op->order;
|
||||
if(!$order || !$order->id) return true;
|
||||
|
||||
foreach($order->files as $file) {
|
||||
$cfile = ContractFileModel::create([
|
||||
"contract_id" => $this->id,
|
||||
"file_id" => $file->file_id,
|
||||
"name" => $file->name,
|
||||
"description" => $file->description,
|
||||
"create_by" => $file->create_by,
|
||||
"edit_by" => $file->edit_by
|
||||
]);
|
||||
$cfile->save();
|
||||
|
||||
$journal = ContractjournalModel::create([
|
||||
"contract_id" => $this->id,
|
||||
"type" => "file",
|
||||
"value" => $cfile->id,
|
||||
"text" => (array_key_exists($cfile->name, TT_ORDER_FILE_TYPES)) ? TT_ORDER_FILE_TYPES[$cfile->name] : $cfile->name
|
||||
]);
|
||||
$journal->save();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
@@ -227,7 +266,7 @@ class Contract extends mfBaseModel {
|
||||
}
|
||||
|
||||
if($name == "links") {
|
||||
$this->links = ContractLinkModel::includesContractId($this->id, ["type" => $link]);
|
||||
$this->links = ContractLinkModel::includesContractId($this->id, ["type" => "link"]);
|
||||
//var_dump($this->links);exit;
|
||||
return $this->links;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class ContractModel {
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function createFromOrderproduct($op) {
|
||||
public static function createFromOrderproduct(OrderProduct $op) {
|
||||
$log = mfLoghandler::singleton();
|
||||
|
||||
if(!$op->id) {
|
||||
@@ -107,6 +107,82 @@ class ContractModel {
|
||||
|
||||
return $contract;
|
||||
}
|
||||
|
||||
public static function createFromContractQueue(Contractqueue $cq, $contracttype = "contract") {
|
||||
$log = mfLoghandler::singleton();
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
|
||||
if(!$cq->id) {
|
||||
$log->warning(__METHOD__."(): Invalid Contractqueue object");
|
||||
return false;
|
||||
}
|
||||
|
||||
$order_id = $cq->order_id;
|
||||
$product_id = $cq->product_id;
|
||||
|
||||
$order = new Order($order_id);
|
||||
$product = new Product($product_id);
|
||||
|
||||
if(!$order->id || !$product->id) {
|
||||
$log->warning(__METHOD__."(): Invalid Order or Product");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$order->finish_date || $order->finish_date > date("U")) {
|
||||
$log->warning(__METHOD__."(): Order not finished yet");
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data["orderproduct_id"] = $cq->orderproduct_id;
|
||||
$data["matchcode"] = $cq->matchcode;
|
||||
$data["owner_id"] = $cq->owner_id;
|
||||
$data["billingaddress_id"] = ($cq->billingaddress_id) ? $cq->billingaddress_id : $cq->owner_id;
|
||||
$data["termination_id"] = ($cq->termination_id) ? $cq->termination_id : null;
|
||||
$data["product_id"] = $cq->product_id;
|
||||
$data["product_name"] = $cq->product_name;
|
||||
$data["product_info"] = $cq->product_info;
|
||||
$data["amount"] = $cq->amount;
|
||||
$data["sla_id"] = $cq->sla_id;
|
||||
$data["product_external"] = $cq->product_external;
|
||||
$data["product_external_id"] = $cq->product_external_id;
|
||||
$data["price"] = $cq->price;
|
||||
$data["price_setup"] = $cq->price_setup;
|
||||
$data["price_nne"] = $cq->price_nne;
|
||||
$data["price_nbe"] = $cq->price_nbe;
|
||||
$data["billing_delay"] = $cq->billing_delay;
|
||||
$data["billing_period"] = $cq->billing_period;
|
||||
$data["contract_term"] = $cq->contract_term;
|
||||
$data["order_date"] = $order->order_date;
|
||||
|
||||
|
||||
$data["finish_date"] = $order->finish_date;
|
||||
$data["finish_date_by"] = $me->id;
|
||||
$data["note"] = $order->note;
|
||||
|
||||
if($contracttype == "credit") {
|
||||
$crediting_price = $cq->price_nne;
|
||||
if($cq->crediting_partner_rate) {
|
||||
$crediting_price = round($cq->price / 100 * $cq->crediting_partner_rate, 4);
|
||||
}
|
||||
$crediting_price *= -1;
|
||||
|
||||
$data["matchcode"] = $cq->crediting_matchcode;
|
||||
$data["owner_id"] = $cq->crediting_partner_id;
|
||||
$data["billingaddress_id"] = $cq->crediting_partner_id;
|
||||
$data["termination_id"] = null;
|
||||
$data["price"] = $crediting_price;
|
||||
$data["price_setup"] = 0;
|
||||
$data["price_nne"] = 0;
|
||||
$data["price_nbe"] = 0;
|
||||
}
|
||||
|
||||
$contract = ContractModel::create($data);
|
||||
//var_dump($contract);exit;
|
||||
|
||||
return $contract;
|
||||
}
|
||||
|
||||
public function savePrecontract($contract) {
|
||||
|
||||
|
||||
@@ -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"];
|
||||
}
|
||||
}
|
||||
@@ -236,7 +236,7 @@ class ContractqueueModel {
|
||||
if(is_array($limit) && count($limit)) {
|
||||
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
|
||||
} elseif(is_numeric($count)) {
|
||||
} elseif(is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT ".$limit['count'];
|
||||
}
|
||||
}
|
||||
@@ -266,7 +266,16 @@ class ContractqueueModel {
|
||||
$where .= " AND Contractqueue.id like '%$id%'";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(array_key_exists("approved", $filter)) {
|
||||
$approved = $filter['approved'];
|
||||
if($approved) {
|
||||
$where .= " AND Contractqueue.approved = 1";
|
||||
} else {
|
||||
$where .= " AND Contractqueue.approved = 0";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("contract_id", $filter)) {
|
||||
$contract_id = $filter['contract_id'];
|
||||
if(is_numeric($contract_id)) {
|
||||
|
||||
Reference in New Issue
Block a user