1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
<?php
class Order extends BaseModel { protected $table = "order";
public function customer() { return $this->belongsTo('Customer'); }
public function orderItems() { return $this->hasMany('OrderItem'); }
public function currency() { return $this->belongsTo('Currency', 'currency_code', 'iso_code'); }
public function shipments() { return $this->hasMany('Shipment'); }
public function payments() { return $this->hasMany('PaymentRecord')->where('deleted', 0)->where('paystatus', '>', 0); }
// the only one success payment public function payment() { return $this->hasOne('PaymentRecord')->where('deleted', 0)->where('paystatus', '>', 0); }
public function statusMaster() { return $this->belongsTo('MasterTypeCode', 'status', 'code')->where('typeid', 'ORDER_STATUS'); }
public function customerTempOrderItems() { return OrderItem::whereDeleted(0)->where('customer_id', $this->customer_id)->where('temp', 1)->get(); }
public function supplierOrders() { return $this->hasMany('SupplierOrder'); }
public function calcAll() { $this->calcItemTotal() ->calcTotalAmount(); return $this; }
public function calcItemTotal() { $total = 0; foreach ($this->orderItems as $orderItem) { $total += $orderItem->discounted_price; } $this->item_total = $total; return $this; }
public function calcTotalAmount() { $this->total_amount = $this->item_total - $vip_grade_deduct - $points_deduct; return $this; }
/** * create OrderItemPhase, InvoiceItem, Invoice * @return Invoice Invoice with InvoiceItems and OrderItemPhase */ public function createInvoice() { $this->generateCode()->save(); // vdump($this->id, get_order_item($this->customer_id, null, 1)); //exit; $invoice = Invoice::create([ 'order_id' => $this->id, 'customer_id' => $this->customer_id, 'purchase_date' => timestamp(), // 'code' => MasterSetting::get('ORDER_NUMBER_PREFIX') . date("YmdHis") . "-D{$this->id}", // 'code' => $this->code . "-SALES", 'amount' => $this->amount, 'currency_code' => $this->currency_code, 'currency_rate' => $this->currency_rate, 'docdate' => timestamp(), 'status' => 'NEW', ]);
foreach (get_order_item($this->customer_id, null, 1) as $orderItem) { // create an orderItemPhase for each invoiceItem $orderItemPhase = OrderItemPhase::create([ 'order_item_id' => $orderItem['order_item_id'], 'amount' => $orderItem['amount'], 'sort' => 1, 'status' => 'NEW', 'name_en' => $orderItem["product_name_en"], 'name_tc' => $orderItem["product_name_tc"], 'name_sc' => $orderItem["product_name_sc"], 'name_idn' => $orderItem["product_name_id"], ]);
// create an invoiceItem for each orderItem $invoiceItem = InvoiceItem::create([ 'invoice_id' => $invoice->id, 'supplier_id' => $orderItem['supplier_id'], 'invoiceable_type' => 'OrderItemPhase', 'invoiceable_id' => $orderItemPhase->id, 'supplier_price' => $orderItem['supplier_price'], 'supplier_currency_code' => $orderItem['supplier_currency_code'], 'price' => $orderItem['order_item_price'], 'quantity' => $orderItem['quantity'], 'discounted_price' => $orderItem['discounted_price'], 'status' => 'NEW', // 'payease_payment_dtl_code' => $orderItem->payease_payment_dtl_code . '-' . $orderItem['order_item_id'] , ]); }
$invoice->generateCode()->calcAmount()->save();
return $invoice; }
public function generateCode() { // code already exist if ($this->code && !$force_update) { return $this; }
if ($this->id) { $this->code = self::generateCodeById($this->id); } return $this; }
public function confirm() { if ($this->temp == 1) { $this->temp = 0; $this->save();
foreach ($this->orderItems as $orderItem) { $orderItem->temp = 0; $orderItem->save(); } } }
public static function generateCodeById($id) { $new_order_number = MasterTxseqno::generate(); $new_order_number = str_pad($new_order_number, 6, "0", STR_PAD_LEFT); return MasterSetting::get('ORDER_NUMBER_PREFIX') . date("y") . $new_order_number; }
public static function statusPaths() { return [ // from, to, role ['NEW', 'OFFER', 'SUPPLIER'], // supplier quote ['OFFER', 'REJECT', 'CUSTOMER'], // customer reject quote ['OFFER', 'ACCEPT', 'CUSTOMER'], // customer accept quote ['REJECT', 'OFFER', 'SUPPLIER'], // supplier new quote ['ACCEPT', 'PAID', 'SYSTEM'], // payment success ['PAID', 'TERMINATED', 'PLATFORM'], // dispute ['PAID', 'IN_PROGRESS', 'SUPPLIER'], // supplier start service ['IN_PROGRESS', 'REVIEW', 'SUPPLIER'], // supplier submit service ['IN_PROGRESS', 'TERMINATED', 'PLATFORM'], //dispute ['REVIEW', 'IN_PROGRESS', 'CUSTOMER'], // customer reject service ['REVIEW', 'TERMINATED', 'PLATFORM'], // dispute ['REVIEW', 'COMPLETE', 'CUSTOMER'], // customer accept service ['COMPLETE', 'PAID', 'SYSTEM'], // NOT USED ['COMPLETE', 'IN_PROGRESS', 'SYSTEM'], // next phase ['COMPLETE', 'ACCEPT', 'SYSTEM'], // next payment phase ]; }
public static function possibleStatus($status, $direction = 'TO', $role = false) { $statuses = []; switch ($direction) { case 'TO': foreach (self::statusPaths() as $path) { if ($status == $path[0]) { if ($role && $path[2] != $role) { continue; } $statuses[] = $path[1]; } } break;
default: foreach (self::statusPaths() as $path) { if ($status == $path[1]) { if ($role && $path[2] != $role) { continue; } $statuses[] = $path[0]; } } break; } return $statuses; } }
|