/var/www/hkosl.com/littleark/webadmin/models/SupplierOrder.php


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
<?php

use Illuminate\Database\Capsule\Manager as DB;
use 
Carbon\Carbon as Carbon;

class 
SupplierOrder extends BaseModel{
    protected 
$table "supplier_order";

    public function 
supplier(){
        return 
$this->belongsTo('Supplier');
    }

    public function 
customer(){
        return 
$this->belongsTo('Customer');
    }

    public function 
order(){
        return 
$this->belongsTo('Order');
    }

    public function 
status_master(){
        return 
$this->belongsTo('MasterTypeCode''status''code')->where('typeid''ORDER_STATUS');
    }

    public function 
currency(){
        return 
$this->belongsTo('Currency''currency_code''iso_code');
    }

    public function 
orderItems(){
        return 
$this->hasMany('OrderItem''supplier_order_id''id');
    }

    public function 
calcAll(){
        
$this->calcItemTotal()
            ->
calcOriTotalAmount()
            ->
calcTotalAmount();
        return 
$this;
    }

    public function 
calcItemTotal(){
        if(
$this->supplier_id){
            
$total 0;
            foreach(
$this->order->orderItems as $orderItem){
                if(
$orderItem->supplier_id == $this->supplier_id){
                    
$total += $orderItem->discounted_price;
                }
            }
            
$this->item_total $total;
        }
        return 
$this;
    }

    public function 
calcOriTotalAmount(){
        
$this->ori_total_amount $this->item_total $this->discount_total;
        return 
$this;
    }

    public function 
calcTotalAmount(){
        if(
$this->supplier->id && $this->supplier->platform_commission_method && $this->supplier->platform_commission_value){
            switch (
$this->supplier->platform_commission_method) {
                case 
'Percent':
                    
$this->total_amount $this->ori_total_amount * (100-$this->supplier->platform_commission_value)/100;
                    break;

                case 
'Amount':
                    
// TODO: check currency
                    
$this->total_amount $this->ori_total_amount $this->supplier->platform_commission_value;
                    break;

                default:
                    throw new 
Exception('Unsupported commission method');
            }
        }
        return 
$this;
    }
}