Untitled

 avatar
augxdev
plain_text
14 days ago
8.1 kB
4
Indexable
Never
public function approveStatus($identifier)
    {
        DB::beginTransaction();
        $bill = $this->findOrFail($identifier);
        if ($bill->status == UNPAID) {
            return responseError('Bill status has already been approved.');
        }
        if ($bill->status == PAID) {
            return responseError('Bill status has already been approved.');
        }
        if ($bill->status == VOID) {
            return responseError('Bill status has already been rendered void.');
        }
        $totalDiscountedAmount = 0;
        $totalAfterTaxAmount = 0;
        $totalDiscount = 0;
        $totalTaxAmount = 0;
        //
        $total_amount_pre_discount = 0;
        $total_amount_pre_discount = array_reduce($bill->products->toArray(), function ($carry, $product) {
            $quantity = $product['pivot']['quantity'];
            $rate = (float)$product['pivot']['rate'];
            return $carry + ($quantity * $rate);
        }, 0);
        //
        foreach ($bill->products as $item) {
            //
            $discount_total = 0;
            $discount_per_product = 0;
            $discount_per_unit = 0;
            $modified_rate = 0;
            $cost_term_amount_all_product = 0;
            $cost_term_amount_per_product = 0;
            $cost_term_amount_per_product_per_unit = 0;
            $cost_term_amount_per_product_total = 0;
            $modified_rate_adc = 0;

            if ($bill->discount_type == 'fixed' || !$bill->discount_type) {
                $discount_per_product = (((($item->pivot->quantity * $item->pivot->rate) / $total_amount_pre_discount) * $bill->discount));
                $discount_per_unit = $discount_per_product / $item->pivot->quantity;
            } elseif ($bill->discount_type == 'percent') {
                $discount_total = $total_amount_pre_discount * ($bill->discount / 100);
                $discount_per_product = (((($item->pivot->quantity * $item->pivot->rate) / $total_amount_pre_discount) * $discount_total));
                $discount_per_unit = $discount_per_product / $item->pivot->quantity;
            }
            $modified_rate  = $item->pivot->rate - $discount_per_unit;

            //
            // foreach ($bill->billProductAdditionalCostTerms as $item_term) {
            //     $product = $this->productInstance->find($item->product_id);
            //     if ($item_term->product_id && !$item_term->type && $product->id == $item_term->product_id) {
            //         $product_term = $this->productInstance->find($item_term->product_id);
            //         $cost_term_amount_per_product = isset($item_term->amount) ? $item_term->amount : 0;
            //         $cost_term_amount_per_product_total += $cost_term_amount_per_product;
            //         $cost_term_amount_per_product_per_unit = $cost_term_amount_per_product / $item['quantity'];
            //         $modified_rate += $cost_term_amount_per_product_per_unit;
            //         $modified_rate_adc = $modified_rate;
            //     } elseif (!$item_term->product_id && $item_term->type) {
            //         $cost_term_amount_all_product = isset($item_term->amount) ? $item_term->amount : 0;
            //         if ($item_term->type == 'value') {
            //             $cost_term_amount_per_product = (((($item->quantity * $item->rate) / $total_amount_pre_discount) * $cost_term_amount_all_product));
            //             $cost_term_amount_per_product_total += $cost_term_amount_per_product;
            //             $cost_term_amount_per_product_per_unit = $cost_term_amount_per_product / $item->quantity;
            //             $modified_rate += $cost_term_amount_per_product_per_unit;
            //             $modified_rate_adc = $modified_rate;
            //         } elseif ($item_term->type == 'quantity') {
            //             // $cost_term_amount_per_product = (((($item->quantity * $item->rate) / $total_amount_pre_discount) * $cost_term_amount_all_product));
            //             // $cost_term_amount_per_product_per_unit = $cost_term_amount_per_product / $item->quantity;
            //             // $modified_rate -= $cost_term_amount_per_product_per_unit;
            //         }
            //     }
            // }
            //
            // dd($discount_per_product);
            $amount = $item->pivot->quantity * $item->pivot->rate;
            $discountedAmount = $amount - $item->pivot->discount;
            $ratioDiscountedAmount = $discountedAmount - $discount_per_product;
            // $ratioDiscountedAmount = $ratioDiscountedAmount - $cost_term_amount_per_product_total;
            $taxAmount = 0;
            if ($item->pivot->tax == 13) {
                // $taxAmount = 13 / 100 * $discountedAmount;
                $taxAmount = 13 / 100 * $ratioDiscountedAmount;
            }
            // $afterTaxAmount = $discountedAmount + $taxAmount;
            $afterTaxAmount = $ratioDiscountedAmount + $taxAmount;
            //

            // // $amount = $item->pivot->quantity * $item->pivot->rate;
            // $amount = $item->pivot->quantity * $item->pivot->modified_rate;
            // $discountedAmount = $amount - $item->pivot->discount ?? 0;
            // $taxAmount = 0;
            // if ($item->pivot->tax == 13) {
            //     $taxAmount = 13 / 100 * $discountedAmount;
            // }

            // ledgerEntry($bill, $discountedAmount, $bill->bill_date, $this->ledgerInstance::DEBITENTRY, $item->pivot->account_head_id, $bill->vendor_id); // product amount
            ledgerEntry($bill, $ratioDiscountedAmount, $bill->bill_date, $this->ledgerInstance::DEBITENTRY, $item->pivot->account_head_id, $bill->vendor_id); // product amount
            if ($taxAmount > 0) {
                ledgerEntry($bill, $taxAmount, $bill->bill_date, $this->ledgerInstance::DEBITENTRY, $this->accountHeadInstance->findBySlug('vat')->id, $bill->vendor_id);
            }
            $totalDiscount = $totalDiscount + $item->pivot->discount;
            $totalDiscountedAmount = $totalDiscountedAmount + $discountedAmount;
            $totalTaxAmount = $totalTaxAmount + $taxAmount;
            $afterTaxAmount = $discountedAmount + $taxAmount;
            $totalAfterTaxAmount = $totalAfterTaxAmount + $afterTaxAmount;

            ledgerEntry($bill, $afterTaxAmount, $bill->bill_date, $this->ledgerInstance::CREDITENTRY, $bill->vendor_id, $item->pivot->account_head_id);

            $product = $this->productInstance->findOrFail($item->pivot->product_id);
            if ($product->inventory != 0) {
                // $inventories = increaseProductRateCalculation($product, $item->pivot->quantity, $item->pivot->rate, $item->pivot->amount);
                $inventories = increaseProductRateCalculation($product, $item->pivot->quantity, $item->pivot->modified_rate, $item->pivot->amount);
                increaseWarehouseCalculation($product, $item->pivot->quantity, $item->pivot->warehouse_id ?? null);
            }
            $item->pivot->update([
                'tracks' => json_encode([
                    'inventory_id' => $inventories->id ?? '',
                    'purchase_quantity' => $item->pivot->quantity,
                    // 'rate' => $item->pivot->rate,
                    'rate' => $item->pivot->modified_rate,
                ])
            ]);
        }

        // foreach ($bill->billProductAdditionalCostTerms as $item_term) {
        //     ledgerEntry($bill, $item_term->amount, $bill->bill_date, $this->ledgerInstance::DEBITENTRY, $item_term->additionalCostTerm->account_head_id, $item_term->paid_account_id);
        //     ledgerEntry($bill, $item_term->amount, $bill->bill_date, $this->ledgerInstance::CREDITENTRY, $item_term->paid_account_id, $item_term->additionalCostTerm->account_head_id);
        // }
        $bill->update(['status' => UNPAID]);
        DB::commit();
        return responseSuccessMsg('Bill status has been changed.');
    }
Leave a Comment