ok

Mini Shell

Direktori : /home2/selectio/www/mm-tailor-billing/app/models/
Upload File :
Current File : //home2/selectio/www/mm-tailor-billing/app/models/Sales_model.php

<?php
 if (!defined('BASEPATH')) {
     exit('No direct script access allowed');
 }

class Sales_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function addPayment($data = [])
    {
        if ($this->db->insert('payments', $data)) {
            if ($data['paid_by'] == 'gift_card') {
                $gc = $this->site->getGiftCard($data['gc_no']);
                $this->db->update('gift_cards', ['balance' => ($gc->balance - $data['amount'])], ['card_no' => $data['gc_no']]);
            }
            $this->syncSalePayments($data['sale_id']);
            error_log("1111");
            return true;
        }
        return false;
    }
    
    public function addPaymentStatus($status, $id)
    {
        error_log("Sale IDS:".$id);
        error_log("Status:".$status);
        
        if ($this->db->update('sales', ['status' => $status], ['id' => $id])) {
            return true;
        }
        return false;
    }
    
    public function addDiscount($id, $discount)
    {
        error_log("Sale IDS:".$id);
        error_log("Discount:".$discount);
        
        if ($this->db->update('sales', ['discount' => $discount], ['id' => $id])) {
            return true;
        }
        return false;
    }
    
    
    public function get_all_cutting_staffs()
    {
        $q = $this->db->get('users');
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }
    
    // Filter Sales Starts Here
    public function getAllCustomers()
    {
        $q = $this->db->get('customers');
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }
    
    public function getAllStaff()
    {
        $q = $this->db->get('users');
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }
    
    
    public function getCuttingUsers()
    {
        
       $where = '(emp_role="cutting unit" or emp_role = "both cutting & stitching")';
       $q = $this->db->get_where('users', $where);
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }
    public function getStitchingUsers()
    {
       $where = '(emp_role="stitching unit" or emp_role = "both cutting & stitching")';
       $q = $this->db->get_where('users', $where);
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }
    
    public function getAllSaleDC()
    {
        $q = $this->db->get('sales');
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }
    
    public function getTotalCustomerSales($customer_id, $user = null, $start_date = null, $end_date = null, $hold_ref = null, $status= null, $payment = null)
    {
        $this->db->select('COUNT(id) as number, sum(grand_total) as amount, sum(paid) as paid');
        if ($start_date && $end_date) {
            $this->db->where('date >=', $start_date);
            $this->db->where('date <=', $end_date);
        }
        if ($user) {
            $this->db->where('created_by', $user);
        }
        if ($hold_ref) {
            $this->db->where('hold_ref', $hold_ref);
        }
        if ($payment) {
            $this->db->where('status', $payment);
        }
        if ($status) {
            $this->db->where('unit_process_status', $status);
        }
        //$this->db->get_where('sales', ['hold_ref' => $hold_ref]);
        if ($this->session->userdata('store_id')) {
            $this->db->where('store_id', $this->session->userdata('store_id'));
        }
        $q = $this->db->get_where('sales', ['customer_id' => $customer_id]);
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }
    
    // Filter Sales Ends Here
    // New Function Starts Here
    public function getSaleByCusID($cus_id) {
        //$this->db->select("(SELECT FORMAT((SUM(tec_sale_items.quantity)),0) from tec_sale_items where tec_sale_items.sale_id=tec_sales.id and tec_sale_items.product_id=1) as pant_count,(SELECT FORMAT((SUM(tec_sale_items.quantity)),0) from tec_sale_items where tec_sale_items.sale_id=tec_sales.id and tec_sale_items.product_id=2 or tec_sale_items.sale_id=tec_sales.id and tec_sale_items.product_id=3) as shirt_count, due_date, hold_ref, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, deliver_date, bill_cancel_status, bill_status");
        $this->db->select("(SELECT FORMAT((SUM(tec_sale_items.quantity)),0) from tec_sale_items LEFT JOIN tec_products ON tec_sale_items.product_id=tec_products.id where tec_sale_items.sale_id=tec_sales.id and tec_products.pant_shirt_category=1) as pant_count,(SELECT FORMAT((SUM(tec_sale_items.quantity)),0) from tec_sale_items LEFT JOIN tec_products ON tec_sale_items.product_id=tec_products.id where tec_sale_items.sale_id=tec_sales.id and tec_products.pant_shirt_category=2) as shirt_count, due_date, hold_ref, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, deliver_date, bill_cancel_status, bill_status, dc_prefix");

        $sq = $this->db->get_where('sales', array('id' => $cus_id), 1);
          if( $sq->num_rows() > 0 ) {
            return $sq->row();
          }
          return FALSE;
    }
    
    public function getSaleIemsBySaleID($sales_id) {
        $this->db->select('sale_items.sale_id,sale_items.quantity,sale_items.product_name,sale_items.product_type,sale_items.cutting_status,sale_items.stitching_status,sale_items.ready_products_status,sale_items.delivery_date_status,sale_items.product_id,sale_items.id,(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=1 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id) as cutting_assigned,
(tec_sale_items.quantity-(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=1 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id)) as remaing_cutting_qty,
(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=2 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id) as stitching_assigned,
(tec_sale_items.quantity-(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=2 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id)) as remaing_stitching_qty,
(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=3 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id) as ready_to_delivery_assigned,
(tec_sale_items.quantity-(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=3 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id)) as remaining_ready_to_delivery_qty,
(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=4 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id) as delivery_date_assigned_products,
(tec_sale_items.quantity-(SELECT sum(tec_work_units.quantity) from tec_work_units where tec_work_units.sale_id=tec_sale_items.sale_id and tec_work_units.type=4 and tec_work_units.assign_or_not="assign" and tec_work_units.product_id=tec_sale_items.product_id)) as remaining_delivery_date_products, (select pant_shirt_category from `tec_products` where tec_products.id=tec_sale_items.product_id) as pant_or_shirt');
          
            
  
            
        $sq = $this->db->get_where('sale_items', ['sale_id' => $sales_id]);
          if($sq->num_rows() > 0) {
            foreach (($sq->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
          return FALSE;
    }
    
    public function getSaleWorkUnitsTable($sales_id) {
        /*$this->db->select('sale_items.*, users.last_name as user_lname, users.first_name as user_fname, products.name as product_name')
            ->join('users', 'sale_items.cutting_user=users.id', 'left')
            ->join('products', 'sale_items.product_id=products.id', 'left');*/
            
        $sq = $this->db->get_where('work_units', ['sale_id' => $sales_id]);
          if($sq->num_rows() > 0) {
            foreach (($sq->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
          return FALSE;
    }
    
    
    public function getSaleIemsBySaleIDWithWorksTable($s_id) {
               
        $sq = $this->db->get_where('works', ['sale_id' => $s_id]);
          if($sq->num_rows() > 0) {
            foreach (($sq->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
          return FALSE;
    }
    
    public function getSaleMainTable($s_id) {
               
        $sq = $this->db->get_where('sales', ['id' => $s_id]);
          if($sq->num_rows() > 0) {
            foreach (($sq->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
          return FALSE;
    }
    
    public function getSaleDetailsByID($sale_id) {
               
        $sq = $this->db->get_where('sales', ['id' => $sale_id]);
          if($sq->num_rows() > 0) {
            foreach (($sq->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
          return FALSE;
    }
    
    // New Function Ends Here

    public function deleteInvoice($id)
    {
        $osale  = $this->getSaleByID($id);
        $oitems = $this->getAllSaleItems($id);
        foreach ($oitems as $oitem) {
            $product = $this->site->getProductByID($oitem->product_id, $osale->store_id);
            if ($product->type == 'standard') {
                $this->db->update('product_store_qty', ['quantity' => ($product->quantity + $oitem->quantity)], ['product_id' => $product->id, 'store_id' => $osale->store_id]);
            } elseif ($product->type == 'combo') {
                $combo_items = $this->getComboItemsByPID($product->id);
                foreach ($combo_items as $combo_item) {
                    $cpr = $this->site->getProductByID($combo_item->id, $osale->store_id);
                    if ($cpr->type == 'standard') {
                        $qty = $combo_item->qty * $oitem->quantity;
                        $this->db->update('product_store_qty', ['quantity' => ($cpr->quantity + $qty)], ['product_id' => $cpr->id, 'store_id' => $osale->store_id]);
                    }
                }
            }
        }
        if ($this->db->delete('sale_items', ['sale_id' => $id]) && $this->db->delete('sales', ['id' => $id]) && $this->db->delete('payments', ['sale_id' => $id]) && $this->db->delete('work_units', ['sale_id' => $id])) {
            return true;
        }
        return false;
    }
    
    /*orginal 
    public function deleteInvoice($id)
    {
        $osale  = $this->getSaleByID($id);
        $oitems = $this->getAllSaleItems($id);
        foreach ($oitems as $oitem) {
            $product = $this->site->getProductByID($oitem->product_id, $osale->store_id);
            if ($product->type == 'standard') {
                $this->db->update('product_store_qty', ['quantity' => ($product->quantity + $oitem->quantity)], ['product_id' => $product->id, 'store_id' => $osale->store_id]);
            } elseif ($product->type == 'combo') {
                $combo_items = $this->getComboItemsByPID($product->id);
                foreach ($combo_items as $combo_item) {
                    $cpr = $this->site->getProductByID($combo_item->id, $osale->store_id);
                    if ($cpr->type == 'standard') {
                        $qty = $combo_item->qty * $oitem->quantity;
                        $this->db->update('product_store_qty', ['quantity' => ($cpr->quantity + $qty)], ['product_id' => $cpr->id, 'store_id' => $osale->store_id]);
                    }
                }
            }
        }
        if ($this->db->delete('sale_items', ['sale_id' => $id]) && $this->db->delete('sales', ['id' => $id]) && $this->db->delete('payments', ['sale_id' => $id])) {
            return true;
        }
        return false;
    }*/
    
    public function deleteAssignList($id)
    {
        if ($this->db->delete('work_units', ['id' => $id])) {
            return true;
        }
        return false;
    }
    
    public function ReadyAllProductsClick($id)
    {
        if ($this->db->update('sales', ['unit_process_status' => '7'], ['id' => $id])) {
            return true;
        }
        return false;
    }

    
    
    public function BulkReadyAllProductsClick($id)
    {
        $sales_id = implode(',', $id);
        error_log("UPDATE tec_sales SET unit_process_status='7' WHERE hold_ref in (".$sales_id.")");
        
        if($sales_id)
        {
            $sql = "UPDATE tec_sales SET unit_process_status='7' WHERE hold_ref in (".$sales_id.")";
            $this->db->query($sql);
            return true;
        }
        return false;
        
        /*if ($this->db->update('sales', ['unit_process_status' => '7'], ['id' => $id])) {
            return true;
        }*/
        /*return false;*/
    }
    
    public function deleteOpenedSale($id)
    {
        if ($this->db->delete('suspended_items', ['suspend_id' => $id]) && $this->db->delete('suspended_sales', ['id' => $id])) {
            return true;
        }
        return false;
    }

    public function deletePayment($id)
    {
        $payment = $this->getPaymentByID($id);
        if ($payment->paid_by == 'gift_card') {
            $gc = $this->site->getGiftCard($payment->gc_no);
            $this->db->update('gift_cards', ['balance' => ($gc->balance + $payment->amount)], ['card_no' => $payment->gc_no]);
        }
        if ($this->db->delete('payments', ['id' => $id])) {
            $this->syncSalePayments($payment->sale_id);
            return true;
        }
        return false;
    }

    public function getAllSaleItems($sale_id)
    {
        $j = "(SELECT id, code, name, tax_method from {$this->db->dbprefix('products')}) P";
        $this->db->select("sale_items.*,
            (CASE WHEN {$this->db->dbprefix('sale_items')}.product_code IS NULL THEN {$this->db->dbprefix('products')}.code ELSE {$this->db->dbprefix('sale_items')}.product_code END) as product_code,
            (CASE WHEN {$this->db->dbprefix('sale_items')}.product_name IS NULL THEN {$this->db->dbprefix('products')}.name ELSE {$this->db->dbprefix('sale_items')}.product_name END) as product_name,
            {$this->db->dbprefix('products')}.tax_method as tax_method", false)
        ->join('products', 'products.id=sale_items.product_id', 'left outer')
        ->order_by('sale_items.id');
        $q = $this->db->get_where('sale_items', ['sale_id' => $sale_id]);
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function getAllSalePayments($sale_id)
    {
        
        $q = $this->db->get_where('payments', ['sale_id' => $sale_id]);
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function getComboItemsByPID($product_id)
    {
        $this->db->select($this->db->dbprefix('products') . '.id as id, ' . $this->db->dbprefix('products') . '.code as code, ' . $this->db->dbprefix('combo_items') . '.quantity as qty, ' . $this->db->dbprefix('products') . '.name as name, ' . $this->db->dbprefix('products') . '.quantity as quantity')
        ->join('products', 'products.code=combo_items.item_code', 'left')
        ->group_by('combo_items.id');
        $q = $this->db->get_where('combo_items', ['product_id' => $product_id]);
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function getCustomerByID($id)
    {
        $q = $this->db->get_where('customers', ['id' => $id], 1);
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }

    public function getPaymentByID($id)
    {
        $q = $this->db->get_where('payments', ['id' => $id], 1);
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }

    public function getSaleByID($id)
    {
        $q = $this->db->get_where('sales', ['id' => $id], 1);
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }

    public function getSalePayments($sale_id)
    {
        $this->db->order_by('id', 'asc');
        $q = $this->db->get_where('payments', ['sale_id' => $sale_id]);
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    public function syncSalePayments($id)
    {
        $sale     = $this->getSaleByID($id);
        $payments = $this->getSalePayments($id);
        $paid     = 0;
        if ($payments) {
            foreach ($payments as $payment) {
                $paid += $payment->amount;
            }
        }
        $status = $paid <= 0 ? 'due' : ($sale->grand_total <= $paid ? 'paid' : 'partial');
        if ($this->db->update('sales', ['paid' => $paid, 'status' => $status], ['id' => $id])) {
            return true;
        }

        return false;
    }

    public function updatePayment($id, $data = [])
    {
        $payment = $this->getPaymentByID($id);
        if ($payment->paid_by == 'gift_card') {
            $gc = $this->site->getGiftCard($payment->gc_no);
            $this->db->update('gift_cards', ['balance' => ($gc->balance + $payment->amount)], ['card_no' => $payment->gc_no]);
        }
        if ($this->db->update('payments', $data, ['id' => $id])) {
            if ($data['paid_by'] == 'gift_card') {
                $gc = $this->site->getGiftCard($data['gc_no']);
                $this->db->update('gift_cards', ['balance' => ($gc->balance - $data['amount'])], ['card_no' => $data['gc_no']]);
            }
            $this->syncSalePayments($data['sale_id']);
            return true;
        }
        return false;
    }

    public function updateStatus($id, $status)
    {
        if ($this->db->update('sales', ['status' => $status], ['id' => $id])) {
            return true;
        }
        return false;
    }
    
    public function overAllDeliveryStatus($sale_id, $full_delivery_date, $full_delivery_date_status)
    {
        
        if($full_delivery_date_status==9){
                if($full_delivery_date==""){
                    $this->db->update('tec_sales', ['unit_process_status' => '9'], ['id' => $sale_id]);
                    error_log(99);
                }else{
                    $this->db->update('tec_sales', ['unit_process_status' => '9', 'deliver_date' => $full_delivery_date], ['id' => $sale_id]);
                    error_log($full_delivery_date);
                }
        }
        
    }
    public function billCancelStatus($sale_id, $bill_cancel_status, $bill_cancell_done)
    {
        
        if($bill_cancel_status==10){
                if($bill_cancell_done==""){
                    
                }else{
                    $this->db->update('tec_sales', ['unit_process_status' => '10', 'bill_status' => $bill_cancell_done], ['id' => $sale_id]);
                    error_log($bill_cancell_done);
                }
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // (1) New Cutting Assign Starts Here 
    //public function cuttingUnitStatus($cutting_sale_id, $cutting_products, $cutting_products_name, $cutting_username, $cutting_assign_date,$cutting_quantity, $cutting_total_quantity, $cutting_status, $overall_delivery_date, $update_work_id,$new_status,$works_cutting_products,$works_cutting_products_name,$works_cutting_username,$works_cutting_assign_date,$works_cutting_quantity)
    
    public function cuttingUnitStatus($cutting_sale_id, $cutting_status, $cutting_products, $cutting_products_name, $cutting_username, $cutting_assign_date, $cutting_quantity, $cutting_total_quantity, $update_work_id, $new_status, $works_cutting_products, $works_cutting_products_name, $works_cutting_username, $works_cutting_assign_date, $works_cutting_quantity, $works_cutting_total_quantity,$overall_total_product_quantity,$overall_assigned_product_quantity)
    {
        
        // Processs 1 Here
        if(in_array("0", $cutting_username) || in_array("", $cutting_assign_date)) {
            if($cutting_status==2){
                $this->db->update('tec_sales', ['unit_process_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sales', ['cutting_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sale_items', ['cutting_status' => '0'], ['sale_id' => $cutting_sale_id]);
            }
        }else{
            if($cutting_status==2){
                $this->db->update('tec_sales', ['unit_process_status' => '3'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sales', ['cutting_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sale_items', ['cutting_status' => '0'], ['sale_id' => $cutting_sale_id]);
            }
            if($cutting_status==22){
                $this->db->update('tec_sales', ['unit_process_status' => '3'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sales', ['cutting_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sale_items', ['cutting_status' => '0'], ['sale_id' => $cutting_sale_id]);
            }
        }
        
        // Processs 2 Here
        $fully_assigned=1;
        for($w=0;$w<count($overall_total_product_quantity);$w++)
        {
            if($overall_total_product_quantity[$w]!=$overall_assigned_product_quantity[$w] && $fully_assigned==1)
            {       
                $fully_assigned=0;
            }
        }
        
        if($fully_assigned==1){
            error_log("Equal It is Working");
            //Fully Assigned
            if($cutting_status==2){
                $this->db->update('tec_sales', ['unit_process_status' => '3'], ['id' => $cutting_sale_id]); 
            }
            if($cutting_status==22){
                $this->db->update('tec_sales', ['unit_process_status' => '3'], ['id' => $cutting_sale_id]); 
            }
        }else{
            error_log("Not Equal It is Working");
            //Partially Assigned
            if($cutting_status==2){
                $this->db->update('tec_sales', ['unit_process_status' => '2'], ['id' => $cutting_sale_id]); 
            }
            if($cutting_status==22){
                $this->db->update('tec_sales', ['unit_process_status' => '2'], ['id' => $cutting_sale_id]); 
            }
        }
        
        // Processs 3 Here
        for($w=0;$w<count($cutting_products);$w++)
        {
            if($cutting_status==2){
                $this->db->insert('tec_work_units', ['type' => '1', 'assign_or_not' => 'assign', 'sale_id' => $cutting_sale_id, 'product_id' => $cutting_products[$w], 'product_name' => $cutting_products_name[$w], 'quantity' => $cutting_quantity[$w], 'total_qty' => $cutting_total_quantity[$w], 'cutting_user_id' => $cutting_username[$w], 'cutting_assign_date' => $cutting_assign_date[$w]]);
            }
            if($cutting_status==22){
                $this->db->update('tec_work_units', ['assign_or_not' => 'assign', 'quantity' => $cutting_quantity[$w],'cutting_user_id' => $cutting_username[$w], 'cutting_assign_date' => $cutting_assign_date[$w]], ['id' => $update_work_id[$w]]);
            }
            /*if($new_status[$w]=="0"){
                if($cutting_status==22){
                    $this->db->insert('tec_work_units', ['type' => '1', 'assign_or_not' => 'assign', 'sale_id' => $cutting_sale_id, 'product_id' => $cutting_products[$w], 'product_name' => $cutting_products_name[$w], 'quantity' => $cutting_quantity[$w], 'total_qty' => $cutting_total_quantity[$w], 'cutting_user_id' => $cutting_username[$w], 'cutting_assign_date' => $cutting_assign_date[$w]]);
                }    
            }*/
        }
        
        // Processs 4 Here
        for($w=0;$w<count($works_cutting_products);$w++)
        {
            if($new_status[$w]=="0"){
                if($cutting_status==22){
                    error_log($works_cutting_products_name[$w]);
                    $this->db->insert('tec_work_units', ['type' => '1', 'assign_or_not' => 'assign', 'sale_id' => $cutting_sale_id, 'product_id' => $works_cutting_products[$w], 'product_name' => $works_cutting_products_name[$w], 'quantity' => $works_cutting_quantity[$w], 'total_qty' => $works_cutting_total_quantity[$w], 'cutting_user_id' => $works_cutting_username[$w], 'cutting_assign_date' => $works_cutting_assign_date[$w]]);
                }    
            }
        }
        return true;
    }
    // (1) New Cutting Assign Ends Here
    
    // (2) New Stithching Like Cutting Assign Starts Here 
    public function stitchingStatus($cutting_sale_id, $cutting_status, $cutting_products, $cutting_products_name, $cutting_username, $cutting_assign_date, $cutting_quantity, $cutting_total_quantity, $update_work_id, $new_status, $works_cutting_products, $works_cutting_products_name, $works_cutting_username, $works_cutting_assign_date, $works_cutting_quantity, $works_cutting_total_quantity,$overall_total_product_quantity,$overall_assigned_product_quantity)
    {
        error_log("Stitching Update Working");
        error_log("SAle Id".$cutting_sale_id);
        
        // Processs 1 Here
        if(in_array("0", $cutting_username) || in_array("", $cutting_assign_date)) {
            if($cutting_status==4){
                $this->db->update('tec_sales', ['unit_process_status' => '4'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sales', ['stitching_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sale_items', ['stitching_status' => '0'], ['sale_id' => $cutting_sale_id]);
            }
        }else{
            if($cutting_status==4){
                $this->db->update('tec_sales', ['unit_process_status' => '5'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sales', ['stitching_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sale_items', ['stitching_status' => '0'], ['sale_id' => $cutting_sale_id]);
            }
            if($cutting_status==44){
                $this->db->update('tec_sales', ['unit_process_status' => '5'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sales', ['stitching_status' => '2'], ['id' => $cutting_sale_id]);
                $this->db->update('tec_sale_items', ['stitching_status' => '0'], ['sale_id' => $cutting_sale_id]);
            }
        }
        
        /*for($w=0;$w<count($cutting_products);$w++)
        {
            if($cutting_status==4){
                if($cutting_quantity[$w]==$cutting_total_quantity[$w]){
                    $this->db->update('tec_sales', ['unit_process_status' => '5'], ['id' => $cutting_sale_id]); 
                }else{
                    $this->db->update('tec_sales', ['unit_process_status' => '4'], ['id' => $cutting_sale_id]); 
                }       
            }
        }*/
        
        
        // Processs 2 Here
        $fully_assigned=1;
        for($w=0;$w<count($overall_total_product_quantity);$w++)
        {
            if($overall_total_product_quantity[$w]!=$overall_assigned_product_quantity[$w] && $fully_assigned==1)
            {       
                $fully_assigned=0;
            }
        }
        
        if($fully_assigned==1){
            error_log("Equal It is Working");
            //Fully Assigned
            if($cutting_status==4){
                $this->db->update('tec_sales', ['unit_process_status' => '5'], ['id' => $cutting_sale_id]); 
            }
            if($cutting_status==44){
                $this->db->update('tec_sales', ['unit_process_status' => '5'], ['id' => $cutting_sale_id]); 
            }
        }else{
            error_log("Not Equal It is Working");
            //Partially Assigned
            if($cutting_status==4){
                $this->db->update('tec_sales', ['unit_process_status' => '4'], ['id' => $cutting_sale_id]); 
            }
            if($cutting_status==44){
                $this->db->update('tec_sales', ['unit_process_status' => '4'], ['id' => $cutting_sale_id]); 
            }
        }
        
        
        
        // Processs 3 Here
        for($w=0;$w<count($cutting_products);$w++)
        {
            if($cutting_status==4){
                $this->db->insert('tec_work_units', ['type' => '2', 'assign_or_not' => 'assign', 'sale_id' => $cutting_sale_id, 'product_id' => $cutting_products[$w], 'product_name' => $cutting_products_name[$w], 'quantity' => $cutting_quantity[$w], 'total_qty' => $cutting_total_quantity[$w], 'stitching_user_id' => $cutting_username[$w], 'stitching_assign_date' => $cutting_assign_date[$w]]);
            }
            if($cutting_status==44){
                $this->db->update('tec_work_units', ['assign_or_not' => 'assign', 'quantity' => $cutting_quantity[$w],'stitching_user_id' => $cutting_username[$w], 'stitching_assign_date' => $cutting_assign_date[$w]], ['id' => $update_work_id[$w]]);
            }
        }
        
        // Processs 4 Here
        for($w=0;$w<count($works_cutting_products);$w++)
        {
            if($new_status[$w]=="0"){
                if($cutting_status==44){
                    error_log($works_cutting_products_name[$w]);
                    $this->db->insert('tec_work_units', ['type' => '2', 'assign_or_not' => 'assign', 'sale_id' => $cutting_sale_id, 'product_id' => $works_cutting_products[$w], 'product_name' => $works_cutting_products_name[$w], 'quantity' => $works_cutting_quantity[$w], 'total_qty' => $works_cutting_total_quantity[$w], 'stitching_user_id' => $works_cutting_username[$w], 'stitching_assign_date' => $works_cutting_assign_date[$w]]);
                }    
            }
        }
        
        return true;
    }
    // (2) New Stitching Like Cutting Assign Ends Here
    
    
    
    
    // (3) **************************** Latest Unit Process Status Starts Here  ****************************************** //
    public function readyToDeliveryStatus($sale_id, $products, $products_name, $quantity, $total_quantity, $ready_or_not_ready, $update_work_id, $measurement_status_insert, $measurement_status_update, $new_status, $new_products,$new_products_name,$new_quantity,$new_total_quantity,$new_ready_or_not_ready,$overall_total_product_quantity,$overall_assigned_product_quantity)
    {
        error_log("Sale Id: ".$sale_id);
        error_log("Ready To Delivery Measurements Insert: ".$measurement_status_insert);
        error_log("Ready To Delivery Measurements Update: ".$measurement_status_update);
        
        
        if(in_array("not_ready", $ready_or_not_ready)) {
            if($measurement_status_insert==6){
            $this->db->update('tec_sales', ['unit_process_status' => '6'], ['id' => $sale_id]);
            $this->db->update('tec_sales', ['ready_to_delivery_status' => '2'], ['id' => $sale_id]);
            $this->db->update('tec_sale_items', ['ready_products_status' => '0'], ['sale_id' => $sale_id]);
            error_log("Ready To Delivery Not Ready");
            }
            if($measurement_status_update==66){
            $this->db->update('tec_sales', ['unit_process_status' => '6'], ['id' => $sale_id]);
            $this->db->update('tec_sales', ['ready_to_delivery_status' => '2'], ['id' => $sale_id]);
            $this->db->update('tec_sale_items', ['ready_products_status' => '0'], ['sale_id' => $sale_id]);
            error_log("Ready To Delivery Not Ready");
            }
        }else{
            if($measurement_status_insert==6){
            $this->db->update('tec_sales', ['unit_process_status' => '7'], ['id' => $sale_id]);
            $this->db->update('tec_sales', ['ready_to_delivery_status' => '2'], ['id' => $sale_id]);
            $this->db->update('tec_sale_items', ['ready_products_status' => '0'], ['sale_id' => $sale_id]);
            error_log("All Ready Ready To Delivery");
            }
            if($measurement_status_update==66){
            $this->db->update('tec_sales', ['unit_process_status' => '7'], ['id' => $sale_id]);
            $this->db->update('tec_sales', ['ready_to_delivery_status' => '2'], ['id' => $sale_id]);
            $this->db->update('tec_sale_items', ['ready_products_status' => '0'], ['sale_id' => $sale_id]);
            error_log("All Ready Ready To Delivery");
            }
        }
        
        
        // Processs 2 Here
        $fully_assigned=1;
        for($w=0;$w<count($overall_total_product_quantity);$w++)
        {
            error_log("NEW Total Products READY".$overall_total_product_quantity[$w]);
            error_log("NEW Total Products ASSIGNED".$overall_assigned_product_quantity[$w]);
            if($overall_total_product_quantity[$w]!=$overall_assigned_product_quantity[$w] && $fully_assigned==1)
            {       
                $fully_assigned=0;
            }
        }
        
        if($fully_assigned==1){
            error_log("Equal It is Working");
            //Fully Assigned
            
            if($measurement_status_insert==6){
                $this->db->update('tec_sales', ['unit_process_status' => '7'], ['id' => $sale_id]);
            }
            if($measurement_status_update==66){
                $this->db->update('tec_sales', ['unit_process_status' => '7'], ['id' => $sale_id]); 
            }
        }else{
            error_log("Not Equal It is Working");
            //Partially Assigned
            
            if($measurement_status_insert==6){
                $this->db->update('tec_sales', ['unit_process_status' => '6'], ['id' => $sale_id]);
            }
            if($measurement_status_update==66){
                $this->db->update('tec_sales', ['unit_process_status' => '6'], ['id' => $sale_id]);
                error_log("It is Working Sales Update 6");
            }
        }
        
        
        for($w=0;$w<count($products);$w++)
        {
            error_log("Sale Id: ".$sale_id);
            error_log("Product Id: ".$products[$w]);
            error_log("Product Name: ".$products_name[$w]);
            error_log("Quantity: ".$quantity[$w]);
            error_log("Total Quantity: ".$total_quantity[$w]);
            error_log("Measurements Insert: ".$measurement_status_insert);
            error_log("Measurements Update: ".$measurement_status_update);
            error_log("Ready Or Not Ready: ".$ready_or_not_ready[$w]);
            error_log("Update Work ID: ".$update_work_id[$w]);
            if($measurement_status_insert==6){
                $this->db->insert('tec_work_units', ['type' => '3', 'assign_or_not' => 'assign', 'sale_id' => $sale_id, 'product_id' => $products[$w], 'product_name' => $products_name[$w], 'quantity' => $quantity[$w], 'total_qty' => $total_quantity[$w], 'ready_or_not_ready' => $ready_or_not_ready[$w]]);
            }
            if($measurement_status_update==66){
                $this->db->update('tec_work_units', ['assign_or_not' => 'assign', 'ready_or_not_ready' => $ready_or_not_ready[$w]], ['id' => $update_work_id[$w]]);
            }
        }
        
        for($w=0;$w<count($new_products);$w++)
        {
            error_log("New Product Count");
            if($new_status[$w]=="0")
            {
                if($measurement_status_update==66){
                    $this->db->insert('tec_work_units', ['type' => '3', 'assign_or_not' => 'assign', 'sale_id' => $sale_id, 'product_id' => $new_products[$w], 'product_name' => $new_products_name[$w], 'quantity' => $new_quantity[$w], 'total_qty' => $new_total_quantity[$w], 'ready_or_not_ready' => $new_ready_or_not_ready[$w]]);
                    error_log("NEW REaDY INSERT SUCCESS"); 
                }    
            }
        }
            
        return true;
    }
    // (3) **************************** Latest Unit Process Status Endss Here  ****************************************** //
    
    
    
    
    // (4) **************************** Latest Delivery Date Unit Process Status Starts Here  ****************************************** //
    public function deliveryDateStatus($sale_id, $products, $products_name, $quantity, $total_quantity, $measurement_status, $delivery_date, $update_work_id, $new_status, $new_products,$new_products_name,$new_quantity,$new_total_quantity,$new_delivery_date,$overall_total_product_quantity,$overall_assigned_product_quantity)
    {
        
        if(in_array("", $delivery_date)) {
            if($measurement_status==8){
            $this->db->update('tec_sales', ['unit_process_status' => '8'], ['id' => $sale_id]);
            $this->db->update('tec_sales', ['order_delivery_status' => '2'], ['id' => $sale_id]);
            $this->db->update('tec_sale_items', ['delivery_date_status' => '0'], ['sale_id' => $sale_id]);
            }
        }else{
            if($measurement_status==8){
            $this->db->update('tec_sales', ['unit_process_status' => '9'], ['id' => $sale_id]);
            $this->db->update('tec_sales', ['order_delivery_status' => '2'], ['id' => $sale_id]);
            $this->db->update('tec_sale_items', ['delivery_date_status' => '0'], ['sale_id' => $sale_id]);
            }
        }
        
        
        
        // Processs 2 Here
        $fully_assigned=1;
        for($w=0;$w<count($overall_total_product_quantity);$w++)
        {
            if($overall_total_product_quantity[$w]!=$overall_assigned_product_quantity[$w] && $fully_assigned==1)
            {       
                $fully_assigned=0;
            }
        }
        
        if($fully_assigned==1){
            error_log("Equal It is Working");
            //Fully Assigned
            if($measurement_status==8){
                $this->db->update('tec_sales', ['unit_process_status' => '9'], ['id' => $sale_id]);
            }
            if($measurement_status==88){
                $this->db->update('tec_sales', ['unit_process_status' => '9'], ['id' => $sale_id]); 
            }
        }else{
            error_log("Not Equal It is Working");
            //Partially Assigned
            if($measurement_status==8){
                $this->db->update('tec_sales', ['unit_process_status' => '8'], ['id' => $sale_id]);
            }
            if($measurement_status==88){
                $this->db->update('tec_sales', ['unit_process_status' => '8'], ['id' => $sale_id]);
            }
        }
        
        for($w=0;$w<count($products);$w++)
        {
            error_log("Sale Id: ".$sale_id);
            error_log("Product Id: ".$products[$w]);
            error_log("Product Name: ".$products_name[$w]);
            error_log("Quantity: ".$quantity[$w]);
            error_log("Total Quantity: ".$total_quantity[$w]);
            error_log("Measurements: ".$measurement_status);
            error_log("Delivery Date: ".$delivery_date[$w]);
            error_log("Update Work ID: ".$update_work_id[$w]);
            if($measurement_status==8){
            $this->db->insert('tec_work_units', ['type' => '4', 'assign_or_not' => 'assign', 'sale_id' => $sale_id, 'product_id' => $products[$w], 'product_name' => $products_name[$w], 'quantity' => $quantity[$w], 'total_qty' => $total_quantity[$w], 'delivery_date' => $delivery_date[$w]]);
            }
            if($measurement_status==88){
            $this->db->update('tec_work_units', ['delivery_date' => $delivery_date[$w]], ['id' => $update_work_id[$w]]);
            }
        }
        
        for($w=0;$w<count($new_products);$w++)
        {
            error_log("New Product Count");
            if($new_status[$w]=="0")
            {
                if($measurement_status==88){
                    $this->db->insert('tec_work_units', ['type' => '4', 'assign_or_not' => 'assign', 'sale_id' => $sale_id, 'product_id' => $new_products[$w], 'product_name' => $new_products_name[$w], 'quantity' => $new_quantity[$w], 'total_qty' => $new_total_quantity[$w], 'delivery_date' => $new_delivery_date[$w]]);
                    error_log("NEW REaDY INSERT SUCCESS"); 
                }    
            }
        }
        return true;
    }
    // (4)**************************** Latest Delivery Date Unit Process Status Endss Here  ****************************************** //
    
    
    
    
}

Zerion Mini Shell 1.0