/var/www/hkosl.com/littleark/webadmin/models/InvoiceItem.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
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
<?php

class InvoiceItem extends BaseModel
{
    protected 
$table "invoice_item";

    public function 
invoice()
    {
        return 
$this->belongsTo('Invoice''invoice_id''id');
    }

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

    public function 
refunds()
    {
        return 
$this->hasMany('Refund''invoice_item_id''id');
    }

    public function 
PayeasePaymentDtls()
    {
        return 
$this->hasMany('PayeasePaymentDtl');
    }

    public function 
invoiceable()
    {
        return 
$this->morphTo();
    }

    public function 
payeasePaymentSubmit()
    {
        return 
$this->belongsTo('PayeasePaymentSubmit''payease_payment_submit_id''id');
    }

    public function 
getDiscountAmount()
    {
        return 
$this->price $this->quantity $discounted_price;
    }

    
/**
     * get the price without supplier discount for generate payment to supplier
     * @return decimal amount after supplier discount in base currency
     */
    
public function getSupplierDiscountedBasedPrice()
    {
        
// vdump(__FUNCTION__);
        // if the order has many invoices, the order discounts have to be split
        // assume all order_discounts are is_order_item_discount (not is_order_discount)
        
$amount $this->price $this->quantity;
        
// vdump($this->invoiceable->orderItem->orderDiscounts);
        
foreach ($this->invoiceable->orderItem->orderDiscounts()->where('offer_by''SUPPLIER')->where('is_order_item_discount'1)->get() as $orderDiscount) {
            
// vdump("orderdiscount_id: {$orderDiscount->id}, {$orderDiscount->deduct_amt} off = {$amount}");
            // if ($orderDiscount->offer_by == "SUPPLIER" && $orderDiscount->is_order_item_discount) {
            
$amount -= $orderDiscount->deduct_amt;
            
// }
        
}
        
// $amount -= $this->refund_amount;
        
return $amount;
    }

    
/**
     * get the price without supplier discount for generate payment to supplier
     * @return decimal amount after supplier discount in payment currency
     */
    
public function getSupplierDiscountedPrice()
    {
        return 
$this->getSupplierDiscountedBasedPrice() * $this->invoice->currency_rate;
    }

    
/**
     * Supplier receivable = supplierDiscountPrice (in payment currency @ checkout day) - refund - platform commission
     * @return decimal supplier receivable amount in payment currency
     */
    
public function getSupplierReceivableAmount()
    {
        
$supplierReceivable $this->getSupplierDiscountedPrice(); // * checkout date currency rate
        // vdump('supplierReceivable', $supplierReceivable);
        
foreach ($this->refunds as $refund) {
            foreach (
$refund->paymentRefunds as $paymentRefund) {
                
// vdump("refund {$paymentRefund->amount}");
                
$supplierReceivable -= $paymentRefund->amount// TODO: deduct platform commission
            
}
        }
        
$supplierReceivable $this->supplier->deductCommission($supplierReceivable);
        
// vdump('supplierReceivable', $supplierReceivable);
        
return $supplierReceivable;
    }

    public function 
createCustomerPoint()
    {
        
$customer $this->invoice->customer;

        
$customerPoint CustomerPoint::create([
            
'customer_id' => $customer->id,
            
'points'      => $this->discounted_price,
            
'status'      => 'APPROVED',
            
'table_name'  => get_class($this),
            
'table_refid' => $this->refid,
            
'remark'      => '',
        ]);

        
// sum(CustomerPoint where table='InoivceItem' and refid=this.id)
        
$this->points $this->discounted_price;
        
// $this->save();

        
$customer->calcPoints();

        return 
$this;
    }
}