ok

Mini Shell

Direktori : /proc/self/root/home2/selectio/www/mm-tailor-billing/app/controllers/
Upload File :
Current File : //proc/self/root/home2/selectio/www/mm-tailor-billing/app/controllers/Sales.php

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

/*UPDATE `tec_sales` SET cutting_status="1", stitching_status="1", ready_to_delivery_status="1", order_delivery_status="1";
UPDATE `tec_sales` SET unit_process_status="1";
UPDATE `tec_sale_items` SET cutting_status="1", stitching_status="1", ready_products_status="1", delivery_date_status="1";*/
class Sales extends MY_Controller
{
    public function __construct()
    {
        error_log("AJAY".$_SERVER['PHP_SELF']);
        parent::__construct();

        if (!$this->loggedIn) {
            redirect('login');
        }
        if (!$this->session->userdata('store_id')) {
            $this->session->set_flashdata('warning', lang('please_select_store'));
            redirect('stores');
        }
        $this->load->library('form_validation');
        $this->load->model('sales_model');

        $this->digital_file_types = 'zip|pdf|doc|docx|xls|xlsx|jpg|png|gif';
    }

    public function add_payment($id = null, $cid = null)
    {
        $this->load->helper('security');
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        $this->form_validation->set_rules('amount-paid', lang('amount'), 'required');
        $this->form_validation->set_rules('paid_by', lang('paid_by'), 'required');
        $this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        if ($this->form_validation->run() == true) {
            if ($this->Admin) {
                $date = $this->input->post('date');
            } else {
                $date = date('Y-m-d H:i:s');
            }
            $payment = [
                'date'        => $date,
                'sale_id'     => $id,
                'customer_id' => $cid,
                'paid_discount'  => $this->input->post('paid_or_discount'),
                'reference'   => $this->input->post('reference'),
                'amount'      => $this->input->post('amount-paid'),
                'discount'      => $this->input->post('discount_amount'),
                'paid_by'     => $this->input->post('paid_by'),
                'cheque_no'   => $this->input->post('cheque_no'),
                'gc_no'       => $this->input->post('gift_card_no'),
                'cc_no'       => $this->input->post('pcc_no'),
                'cc_holder'   => $this->input->post('pcc_holder'),
                'cc_month'    => $this->input->post('pcc_month'),
                'cc_year'     => $this->input->post('pcc_year'),
                'cc_type'     => $this->input->post('pcc_type'),
                'note'        => $this->input->post('note'),
                'created_by'  => $this->session->userdata('user_id'),
                'store_id'    => $this->session->userdata('store_id'),
            ];
            
            $status = $this->input->post('paid');
            $discount = $this->input->post('discount_amount');
            

            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'files/';
                $config['allowed_types'] = $this->digital_file_types;
                $config['max_size']      = 2048;
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect($_SERVER['HTTP_REFERER']);
                }
                $photo                 = $this->upload->file_name;
                $payment['attachment'] = $photo;
            }

            // $this->tec->print_arrays($payment);
        } elseif ($this->input->post('add_payment')) {
            $this->session->set_flashdata('error', validation_errors());
            $this->tec->dd();
        }

        if ($this->form_validation->run() == true && $this->sales_model->addPayment($payment)) {
            $this->sales_model->addPaymentStatus($status,$id);
            $this->sales_model->addDiscount($id,$discount);
            
            $response = array(
                    'status' => 'success',
                    'message' => 'Payment Completed'
                    );
            echo json_encode($response);
            return 0;
            
            $this->session->set_flashdata('message', lang('payment_added'));
            redirect($_SERVER['HTTP_REFERER']);
        } else {
            $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $sale                = $this->sales_model->getSaleByID($id);
            $this->data['inv']   = $sale;
            $datee = $this->uri->segment(2);
            /*$this->uri->segment(1) = $idsss;*/
            
            
            $this->load->view($this->theme . 'sales/add_payment', $this->data);
            
            $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
            echo json_encode($response);
            return 0;
            
        }
        echo json_encode($response);
    }

    public function delete($id = null)
    {
        if (DEMO) {
            $this->session->set_flashdata('error', lang('disabled_in_demo'));
            redirect($_SERVER['HTTP_REFERER'] ?? 'welcome');
        }

        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('sales');
        }

        if ($this->sales_model->deleteInvoice($id)) {
            $this->session->set_flashdata('message', lang('invoice_deleted'));
            redirect('sales');
        }
    }
    
    
    public function assigned_delete($id = null)
    {
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('sales');
        }

        if ($this->sales_model->deleteAssignList($id)) {
            $this->session->set_flashdata('message', lang('Assigned List Deleted Successfully'));
            redirect('sales');
        }
    }
    public function delete_holded($id = null)
    {
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect('sales/opened');
        }

        if ($this->sales_model->deleteOpenedSale($id)) {
            $this->session->set_flashdata('message', lang('opened_bill_deleted'));
            redirect('sales/opened');
        }
    }

    public function delete_payment($id = null)
    {
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect($_SERVER['HTTP_REFERER']);
        }

        if ($this->sales_model->deletePayment($id)) {
            $this->session->set_flashdata('message', lang('payment_deleted'));
            redirect('sales');
        }
    }
    
    // New Function Starst Here 
    function customerInfo()
    {
        $customer_id = $this->input->get('sale_id');
        $customer_details = $this->sales_model->getSaleByCusID($customer_id);
        
        echo json_encode($customer_details);
    }
    
    function deleteAssign()
    {
        $id = $this->input->get('id');
        $this->sales_model->deleteAssignList($id);
    }
    
    function readyAllProducts()
    {
        $id = $this->input->get('id');
        $this->sales_model->ReadyAllProductsClick($id);
    }
    
    function BulkReadyAllSales()
    {
        $id = $this->input->get('assign_id');
        $this->sales_model->BulkReadyAllProductsClick($id);
    }
    
    function saleItemsInfo()
    {
        $sales_id = $this->input->get('sale_id');
        $sale_items_details = $this->sales_model->getSaleIemsBySaleID($sales_id);
        
        echo json_encode($sale_items_details);
    }
    
    
    
    
    
    
    
    // New Function Ends Here
    
    public function edit_payment($id = null, $sid = null)
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('error', lang('access_denied'));
            redirect($_SERVER['HTTP_REFERER']);
        }
        $this->load->helper('security');
        if ($this->input->get('id')) {
            $id = $this->input->get('id');
        }

        $this->form_validation->set_rules('amount-paid', lang('amount'), 'required');
        $this->form_validation->set_rules('paid_by', lang('paid_by'), 'required');
        $this->form_validation->set_rules('userfile', lang('attachment'), 'xss_clean');
        if ($this->form_validation->run() == true) {
            $payment = [
                'sale_id'    => $sid,
                'reference'  => $this->input->post('reference'),
                'amount'     => $this->input->post('amount-paid'),
                'paid_by'    => $this->input->post('paid_by'),
                'cheque_no'  => $this->input->post('cheque_no'),
                'gc_no'      => $this->input->post('gift_card_no'),
                'cc_no'      => $this->input->post('pcc_no'),
                'cc_holder'  => $this->input->post('pcc_holder'),
                'cc_month'   => $this->input->post('pcc_month'),
                'cc_year'    => $this->input->post('pcc_year'),
                'cc_type'    => $this->input->post('pcc_type'),
                'note'       => $this->input->post('note'),
                'updated_by' => $this->session->userdata('user_id'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];

            if ($this->Admin) {
                $payment['date'] = $this->input->post('date');
            }

            if ($_FILES['userfile']['size'] > 0) {
                $this->load->library('upload');
                $config['upload_path']   = 'files/';
                $config['allowed_types'] = $this->digital_file_types;
                $config['max_size']      = 2048;
                $config['overwrite']     = false;
                $config['encrypt_name']  = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    redirect($_SERVER['HTTP_REFERER']);
                }
                $photo                 = $this->upload->file_name;
                $payment['attachment'] = $photo;
            }

            //$this->tec->print_arrays($payment);
        } elseif ($this->input->post('edit_payment')) {
            $this->session->set_flashdata('error', validation_errors());
            $this->tec->dd();
        }

        if ($this->form_validation->run() == true && $this->sales_model->updatePayment($id, $payment)) {
            
            $response = array(
                    'status' => 'success',
                    'message' => 'Payment Completed'
                    );
            echo json_encode($response);
            return 0;
            
            $this->session->set_flashdata('message', lang('payment_updated'));
            /*redirect('sales');*/
        } else {
            $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $payment             = $this->sales_model->getPaymentByID($id);
            /*if ($payment->paid_by != 'cash') {
                $this->session->set_flashdata('error', lang('only_cash_can_be_edited'));
                $this->tec->dd();
            }*/
            $this->data['payment'] = $payment;
            $this->load->view($this->theme . 'sales/edit_payment', $this->data);
            
            $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
            echo json_encode($response);
            return 0;
        }
        
         echo json_encode($response);
    }

    public function get_opened_list()
    {
        $this->load->library('datatables');
        if ($this->db->dbdriver == 'sqlite3') {
            $this->datatables->select("id, date, customer_name, hold_ref, (total_items || ' (' || total_quantity || ')') as items, grand_total", false);
        } else {
            $this->datatables->select("id, date, customer_name, hold_ref, CONCAT(total_items, ' (', total_quantity, ')') as items, grand_total", false);
        }
        $this->datatables->from('suspended_sales');
        if (!$this->Admin) {
            $user_id = $this->session->userdata('user_id');
            $this->datatables->where('created_by', $user_id);
        }
        $this->datatables->where('store_id', $this->session->userdata('store_id'));
        $this->datatables->add_column(
            'Actions',
            "<div class='text-center'><div class='btn-group'><a href='" . site_url('pos/?hold=$1') . "' title='" . lang('click_to_add') . "' class='tip btn btn-info btn-xs'><i class='fa fa-th-large'></i></a>
            <a href='" . site_url('sales/delete_holded/$1') . "' onClick=\"return confirm('" . lang('alert_x_holded') . "')\" title='" . lang('delete_sale') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>",
            'id'
        )
        ->unset_column('id');

        echo $this->datatables->generate();
    }

    public function get_sales()
    {
        
        $customer = $this->input->get('customer') ? $this->input->get('customer') : NULL;
        $start_order_date = $this->input->get('start_order_date') ? $this->input->get('start_order_date') : NULL;
        $end_order_date = $this->input->get('end_order_date') ? $this->input->get('end_order_date') : NULL;
        $start_date = $this->input->get('start_date') ? $this->input->get('start_date') : NULL;
        $end_date = $this->input->get('end_date') ? $this->input->get('end_date') : NULL;
        $user = $this->input->get('user') ? $this->input->get('user') : NULL;
        $sells = $this->input->get('sells') ? $this->input->get('sells') : NULL;
        $dc_number_search = $this->input->get('dc_number_search') ? $this->input->get('dc_number_search') : NULL;
        $delivery_status = $this->input->get('delivery_status') ? $this->input->get('delivery_status') : NULL;
        
        
        $hold_ref = $this->input->get('hold_ref'); // Multiple DC Numbers
        $explode_hold_ref = explode(',', $hold_ref);
        
        $status = $this->input->get('process_status') ? $this->input->get('process_status') : NULL;
        $payment = $this->input->get('payment') ? $this->input->get('payment') : NULL;
        
        $start_dc = $this->input->get('start_dc') ? $this->input->get('start_dc') : NULL;
        $end_dc = $this->input->get('end_dc') ? $this->input->get('end_dc') : NULL;
        
        
        $due_date = $this->input->get('due_date') ? $this->input->get('due_date') : NULL;
        $ready_status = $this->input->get('ready_status') ? $this->input->get('ready_status') : NULL;

        $mm_series = $this->input->get('mm_series') ? $this->input->get('mm_series') : NULL;
        
        
        $this->load->library('datatables');
        
        
        
            
        if ($this->db->dbdriver == 'sqlite3') {
            /*$this->datatables->select("tec_sales.id, DATE_FORMAT(date, '%d-%m-%Y') as date, customer_name, customer_phone_no, hold_ref, total, total_tax, total_discount, grand_total, paid, status, DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, total_items,unit_process_status, (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,sells,(grand_total-(paid+discount)) as balance, discount, (SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id) as total_quantity, (SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign') as cutting_assigned_quantity,((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) as cutting_remaining_new,((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))as stitching_remaining_new,IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) as neww_musthafa_cutting,IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_stitching");*/
            $this->datatables->select("tec_sales.id, 
            DATE_FORMAT(date, '%d-%m-%Y') as date, 
            customer_name, customer_phone_no, hold_ref, total, CONCAT(IFNULL(dc_prefix, ''), hold_ref) AS New_DC_number, total_tax, total_discount, grand_total, paid, status, 
            DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, 
            DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, 
            DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, 
            total_items,unit_process_status, 
            
            CASE
    WHEN (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) IS NULL
    THEN 0
    ELSE (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)
  END AS shirt_count,
  CASE
    WHEN (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) IS NULL
    THEN 0
    ELSE (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)
  END 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=1) as pant_counts,
            (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_counts,
            
            
            sells,(grand_total-(paid+discount)) as balance, discount, 
            IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_cutting, 
            IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_stitching,
            (SELECT phone FROM tec_customers WHERE tec_sales.customer_id=tec_customers.id) as customer_phone, 
            (SELECT name FROM tec_customers WHERE tec_sales.customer_id=tec_customers.id) as customer_names, tec_customers.name as cus_name");
            
        } else {
            /*$this->datatables->select("tec_sales.id, DATE_FORMAT(date, '%d-%m-%Y') as date, customer_name, customer_phone_no, hold_ref, total, total_tax, total_discount, grand_total, paid, status, DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, total_items,unit_process_status, (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,sells,(grand_total-paid) as balance, discount");*/        
            /*$this->datatables->select("tec_sales.id, DATE_FORMAT(date, '%d-%m-%Y') as date, customer_name, customer_phone_no, hold_ref, total, total_tax, total_discount, grand_total, paid, status, DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, total_items,unit_process_status, (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,sells,(grand_total-(paid+discount)) as balance, discount, (SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id) as total_quantity, (SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign') as cutting_assigned_quantity,((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) as cutting_remaining_new,((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))as stitching_remaining_new,IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) as neww_musthafa_cutting,IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_stitching");*/
            $this->datatables->select("tec_sales.id, 
            DATE_FORMAT(date, '%d-%m-%Y') as date, 
            customer_name, customer_phone_no, hold_ref, CONCAT(IFNULL(dc_prefix, ''), hold_ref) AS New_DC_number, total, total_tax, total_discount, grand_total, paid, status, 
            DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, 
            DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, 
            DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, 
            total_items,unit_process_status, 
            
            CASE
    WHEN (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) IS NULL
    THEN 0
    ELSE (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)
  END AS shirt_count,
  
  CASE
    WHEN (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) IS NULL
    THEN 0
    ELSE (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)
  END 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=1) as pant_counts,
            (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_counts,
            
            
            sells,(grand_total-(paid+discount)) as balance, discount, 
            IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_cutting, 
            IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_stitching,
            (SELECT phone FROM tec_customers WHERE tec_sales.customer_id=tec_customers.id) as customer_phone, 
            (SELECT name FROM tec_customers WHERE tec_sales.customer_id=tec_customers.id) as customer_names, tec_customers.name as cus_name");
        
            
        }
        
        
        
         /*$this->datatables->select("tec_sales.id, DATE_FORMAT(date, '%d-%m-%Y') as date, customer_name, customer_phone_no, hold_ref, total, total_tax, total_discount, grand_total, paid, status, DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, DATE_FORMAT(sale_date, '%d-%m-%Y') as sale_date, total_items,unit_process_status, (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,sells,(grand_total-(paid+discount)) as balance, discount, (SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id) as total_quantity, (SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign') as cutting_assigned_quantity,((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) as cutting_remaining_new,((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))as stitching_remaining_new,IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) as neww_musthafa_cutting,IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id)-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) as neww_musthafa_stitching");*/
        //IF(sells='1','Sales',IF(sells='2','Trails',IF(sells='3','Trail Completed','Undefined'))) as sale_type_name
        $this->datatables->from('sales');
        
        $this->datatables->join('tec_customers', 'tec_customers.id = tec_sales.customer_id','left');
        /*$this->datatables->corder_by('hold_ref','desc');*/
        if (!$this->Admin && !$this->session->userdata('view_right')) {
            $this->datatables->where('created_by', $this->session->userdata('user_id'));
            /*$this->datatables->where('sells', '1');*/
        }
        $this->datatables->where('tec_sales.store_id', $this->session->userdata('store_id'));
        /*$this->datatables->where('sells', '1');*/
        
        
        /*<a href='" . site_url('pos/view/$1/1') . "' title='" . lang('view_invoice') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax-modal'><i class='fa fa-list'></i></a>*/
        /*<a href='" . site_url('sales/payments/$1') . "' title='" . lang('view_payments') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax'><i class='fa fa-money'></i></a>*/
        //$this->datatables->join('tec_sale_items', 'tec_sales.id = tec_sale_items.sale_id','inner');
        
        $this->datatables->add_column('View', "<div class='text-center'><a href='" . site_url('pos/view/$1') . "' title='" . lang('view_invoice') . "' data-toggle='modal' target='a_blank' class='btn btn-success btn-xs'><i class='fa fa-eye'></i> View</a> </div>", 'id');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><span title='Ready All Products' id='$1' onClick='readyAllproductsClick($1,$2,$3)' class='tip btn btn-success btn-xs ready_check'><i class='fa fa-check'></i></span><span title='Send SMS' id='$1' class='tip btn btn-info btn-xs sms'><i class='fa fa-envelope-o'></i></span><span title='History' id='$1' class='tip btn btn-warning btn-xs view-history'><i class='fa fa-history'></i></span> <a href='" . site_url('pos/?clone=$1') . "' title='Clone' class='tip btn btn-success btn-xs'><i class='fa fa-copy'></i></a> <a href='" . site_url('sales/payments/$1') . "' title='" . lang('view_payments') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax'><i class='fa fa-money'></i></a> <a href='" . site_url('sales/add_payment/$1') . "' title='" . lang('add_payment') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax'><i class='fa fa-briefcase'></i></a> <a href='" . site_url('pos/?edit=$1') . "' target='a_blank' title='" . lang('edit_invoice') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('sales/delete/$1') . "' onClick=\"return confirm('" . lang('alert_x_sale') . "')\" title='" . lang('delete_sale') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id, neww_musthafa_cutting, neww_musthafa_stitching');
        $this->datatables->edit_column('sale_type_name', "<div class='text-center'><b style='color: red;' onClick='ready($1,$2,$3)'><b>$1</div>",'sale_type_name');
        
        $this->datatables->add_column('Ready', "$2$3",'id, neww_musthafa_cutting, neww_musthafa_stitching');

        //$this->datatables->edit_column('Balance', "<div class='text-center'><span class='tip btn btn-danger btn-sm' style='padding: 1px 10px 1px 10px;font-size: 14px;'><i class='fa fa-inr'></i> $1</span></div>",'balance');
        if($customer) { $this->datatables->where('customer_id', $customer); }
        if($user) { $this->datatables->where('created_by', $user); }
        
        
        if($sells) { $this->datatables->where('sells', $sells); }
        
        if($dc_number_search) { $this->datatables->where('hold_ref', $dc_number_search); }
        if($start_order_date) { $this->datatables->where('sale_date >=', $start_order_date); }
        if($end_order_date) { $this->datatables->where('sale_date <=', $end_order_date); }
        
        
        if($start_date) { $this->datatables->where('due_date >=', $start_date); }
        if($end_date) { $this->datatables->where('due_date <=', $end_date); }
        
        
        if($due_date) { $this->datatables->where('due_date >=', $due_date); }
        if($due_date) { $this->datatables->where('due_date <=', $due_date); }
        
        
        if($delivery_status==9) { $this->datatables->where('unit_process_status', $delivery_status); }
        if($delivery_status==99) { $this->datatables->where('unit_process_status !=', 9); $this->datatables->where('unit_process_status !=', 7); }

        if($mm_series==1) { $this->datatables->where('dc_prefix', "A"); }
        if($mm_series==2) { $this->datatables->where('dc_prefix IS NULL'); }
        
        
        if($ready_status==1) {
            
            /*$where ="IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')))>0 or  IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign'))>0";
            $this->datatables->where($where);*/
            
            $where = "(IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) > 0 or IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) > 0) ";
            $this->datatables->where($where);
        }
        if($ready_status==2) {
            $this->datatables->where("IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=2 and tec_work_units.assign_or_not='assign'))) =", '0'); 
            $this->datatables->where("IF(((SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) is NULL,(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id  and product_type='service'),(SELECT sum(tec_sale_items.quantity) FROM tec_sale_items WHERE tec_sale_items.sale_id=tec_sales.id and product_type='service')-(SELECT sum(tec_work_units.quantity) FROM tec_work_units WHERE tec_work_units.sale_id=tec_sales.id and tec_work_units.type=1 and tec_work_units.assign_or_not='assign')) =", '0');
        }
        
        
        if($end_dc && $start_dc) {$this->datatables->where("hold_ref BETWEEN '$start_dc' AND '$end_dc'");}
        
        if($hold_ref) { 
            $hold_ref='['.$hold_ref.']';
            $this->datatables->where_in('hold_ref', $explode_hold_ref); 
        }
        
        
        if($status) { $this->datatables->where('unit_process_status', $status); }
        
        
        if($payment=="paid"){
            $where="((total-(paid+discount))=0)";
            $this->datatables->where($where);
        }
        if($payment=="partial"){
            $where="(total-(paid+discount))>0";
            $this->datatables->where($where);
        }
        
        //if($payment) { $this->datatables->where('status', $payment); }
        
        echo $this->datatables->generate();
    }
    
    

    public function index($id = NULL)
    {
        /*$id = $this->input->post('customer') ? $this->input->post('customer') : NULL;
        // Filter Starts Here 
        if ($this->input->post('process_status')) 
        {
            $start_date = $this->input->post('start_date') ? $this->input->post('start_date') : NULL;
            $end_date = $this->input->post('end_date') ? $this->input->post('end_date') : NULL;
            $user = $this->input->post('user') ? $this->input->post('user') : NULL;
            $hold_ref = $this->input->post('hold_ref') ? $this->input->post('hold_ref') : NULL;
            $status = $this->input->post('process_status') ? $this->input->post('process_status') : NULL;
            $payment = $this->input->post('payment') ? $this->input->post('payment') : NULL;
            $this->data['total_sales'] = $this->sales_model->getTotalCustomerSales($this->input->post('customer'), $user, $start_date, $end_date, $hold_ref, $status, $payment);
        }
        // Filter Ends Here*/ 
        
        $this->data['error']        = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['customers']    = $this->sales_model->getAllCustomers();
        $this->data['users']        = $this->sales_model->getAllStaff();
        /*$this->data['sale_items']   = $this->sales_model->getAllSaleItems(id);*/
        $this->data['dc_number']    = $this->sales_model->getAllSaleDC();
        $this->data['page_title']   = lang('sales');
        $bc                         = [['link' => '#', 'page' => lang('sales')]];
        $meta                       = ['page_title' => lang('sales'), 'bc' => $bc];
        $this->page_construct('sales/index', $this->data, $meta);
    }
    
    
    // Trails Section Starts Here 
    public function trail_sales()
    {
        // Filter Starts Here 
        if ($this->input->post('customer')) 
        {
            $start_date = $this->input->post('start_date') ? $this->input->post('start_date') : NULL;
            $end_date = $this->input->post('end_date') ? $this->input->post('end_date') : NULL;
            $user = $this->input->post('user') ? $this->input->post('user') : NULL;
            $hold_ref = $this->input->post('hold_ref') ? $this->input->post('hold_ref') : NULL;
            $status = $this->input->post('process_status') ? $this->input->post('process_status') : NULL;
            $payment = $this->input->post('payment') ? $this->input->post('payment') : NULL;
            $this->data['total_sales'] = $this->sales_model->getTotalCustomerSales($this->input->post('customer'), $user, $start_date, $end_date, $hold_ref, $status, $payment);
        }
        // Filter Ends Here 
        
        $this->data['error']        = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['customers']    = $this->sales_model->getAllCustomers();
        $this->data['users']        = $this->sales_model->getAllStaff();
        $this->data['sale_items']   = $this->sales_model->getAllSaleItems(id);
        $this->data['page_title']   = lang('trail_sales');
        $bc                         = [['link' => '#', 'page' => lang('trail_sales')]];
        $meta                       = ['page_title' => lang('trail_sales'), 'bc' => $bc];
        $this->page_construct('sales/trail_sales', $this->data, $meta);
    }
    
    public function get_trail_sales()
    {
        $customer = $this->input->get('customer') ? $this->input->get('customer') : NULL;
        $start_date = $this->input->get('start_date') ? $this->input->get('start_date') : NULL;
        $end_date = $this->input->get('end_date') ? $this->input->get('end_date') : NULL;
        $user = $this->input->get('user') ? $this->input->get('user') : NULL;
        $hold_ref = $this->input->get('hold_ref') ? $this->input->get('hold_ref') : NULL;
        $status = $this->input->get('process_status') ? $this->input->get('process_status') : NULL;
        $payment = $this->input->get('payment') ? $this->input->get('payment') : NULL;
        
        $this->load->library('datatables');
        if ($this->db->dbdriver == 'sqlite3') {
            
            $this->datatables->select("id, strftime('%Y-%m-%d %H:%M', date) as date, customer_name, customer_phone_no, hold_ref, total, total_tax, total_discount, grand_total, paid, status, cutting_unit_user, stitching_unit_user, unit_process_status, cutting_unit_com_date, stitching_unit_com_date, deliver_date, DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, total_items");
        } else {
            
            $this->datatables->select("id, DATE_FORMAT(date, '%Y-%m-%d %H:%i') as date, customer_name, customer_phone_no, hold_ref, total, total_tax, total_discount, grand_total, paid, status, cutting_unit_user, stitching_unit_user, unit_process_status, cutting_unit_com_date, stitching_unit_com_date, deliver_date, DATE_FORMAT(deliver_date, '%d-%m-%Y') as deliver_date, DATE_FORMAT(due_date, '%d-%m-%Y') as due_date, total_items");

            }
        
        $this->datatables->from('sales');
        if (!$this->Admin && !$this->session->userdata('view_right')) {
            $this->datatables->where('sells', $this->session->userdata('user_id'));
            $this->datatables->where('sells', '2');
        }
        $this->datatables->where('store_id', $this->session->userdata('store_id'));
        $this->datatables->where('sells', '2');
        
        
        
        $this->datatables->add_column('Clone', "<div class='text-center'><a href='" . site_url('pos/?clone=$1') . "' title='" . lang('view_invoice') . "' target='_blank' class='tip btn btn-success btn-xs'>Clone <i class='fa fa-copy'></i></a> </div>", 'id');
        $this->datatables->add_column('Actions', "<div class='text-center'><div class='btn-group'><a href='" . site_url('pos/view/$1/1') . "' title='" . lang('view_invoice') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax-modal'><i class='fa fa-list'></i></a> <a href='" . site_url('sales/payments/$1') . "' title='" . lang('view_payments') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax'><i class='fa fa-money'></i></a> <a href='" . site_url('sales/add_payment/$1') . "' title='" . lang('add_payment') . "' class='tip btn btn-primary btn-xs' data-toggle='ajax'><i class='fa fa-briefcase'></i></a> <a href='" . site_url('pos/?edit=$1') . "' title='" . lang('edit_invoice') . "' class='tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> <a href='" . site_url('sales/delete/$1') . "' onClick=\"return confirm('" . lang('alert_x_sale') . "')\" title='" . lang('delete_sale') . "' class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a></div></div>", 'id');

        
        if($customer) { $this->datatables->where('customer_id', $customer); }
        if($user) { $this->datatables->where('created_by', $user); }
        if($start_date) { $this->datatables->where('date >=', $start_date); }
        if($end_date) { $this->datatables->where('date <=', $end_date); }
        if($hold_ref) { $this->datatables->where('hold_ref', $hold_ref); }
        if($status) { $this->datatables->where('unit_process_status', $status); }
        if($payment) { $this->datatables->where('status', $payment); }
        
        echo $this->datatables->generate();
    }
    // Trails Section Ends Here 

    public function opened()
    {
        $this->data['error']      = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['page_title'] = lang('opened_bills');
        $bc                       = [['link' => '#', 'page' => lang('opened_bills')]];
        $meta                     = ['page_title' => lang('opened_bills'), 'bc' => $bc];
        $this->page_construct('sales/opened', $this->data, $meta);
    }

    public function payment_note($id = null)
    {
        $payment                  = $this->sales_model->getPaymentByID($id);
        $inv                      = $this->sales_model->getSaleByID($payment->sale_id);
        $this->data['customer']   = $this->site->getCompanyByID($inv->customer_id);
        $this->data['inv']        = $inv;
        $this->data['payment']    = $payment;
        $this->data['page_title'] = $this->lang->line('payment_note');

        $this->load->view($this->theme . 'sales/payment_note', $this->data);
    }

    /* -------------------------------------------------------------------------------- */

    public function payments($id = null)
    {
        $this->data['payments'] = $this->sales_model->getSalePayments($id);
        $this->load->view($this->theme . 'sales/payments', $this->data);
    }

    public function status()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('sale_id', lang('sale_id'), 'required');
        $this->form_validation->set_rules('status', lang('status'), 'required');

        if ($this->form_validation->run() == true) {
            $this->sales_model->updateStatus($this->input->post('sale_id', true), $this->input->post('status', true));
            $this->session->set_flashdata('message', lang('status_updated'));
            redirect('sales');
        } else {
            $this->session->set_flashdata('error', validation_errors());
            redirect('sales');
        }
    }

    
    
    
    
    
    
    
    // New Method Update Measurement Starts Here 
    public function up_status_new()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('cutting_sale_id', lang('cutting_sale_id'), 'required');
        
        
        
        
        if ($this->form_validation->run() == true) {
            
            
            // New Starts
            $cutting_sale_id = $this->input->post('cutting_sale_id', true);
            $cutting_status = $this->input->post('cutting_status');
            
            $cutting_products =array();
            $cutting_products_name=array();
            $cutting_username=array();
            $cutting_assign_date=array();
            $cutting_quantity=array();
            $cutting_total_quantity=array();
            $update_work_id = array();
            $new_status=array();
            
            $works_cutting_products=array();
            $works_cutting_products_name=array();
            $works_cutting_username=array();
            $works_cutting_assign_date=array();
            $works_cutting_quantity=array();
            $works_cutting_total_quantity=array();
            
            $overall_total_product_quantity=array();
            $overall_assigned_product_quantity=array();
            
            $cutting_products = $this->input->post('cutting_products');
            $cutting_products_name = $this->input->post('cutting_products_name');
            $cutting_username = $this->input->post('cutting_user_name');
            $cutting_assign_date = $this->input->post('cutting_assign_date');
            $cutting_quantity = $this->input->post('cutting_quantity');
            $cutting_total_quantity = $this->input->post('cutting_total_quantity');
            
            
            $update_work_id = $this->input->post('cutting_update_works_id');
            $new_status = $this->input->post('new_status');
            
           
            
            $works_cutting_products = $this->input->post('works_cutting_products');
            $works_cutting_products_name = $this->input->post('works_cutting_products_name');
            $works_cutting_username = $this->input->post('works_cutting_username');
            $works_cutting_assign_date = $this->input->post('works_cutting_assign_date');
            $works_cutting_quantity = $this->input->post('works_cutting_quantity');
            $works_cutting_total_quantity = $this->input->post('works_cutting_total_quantity');
            
            
            $overall_total_product_quantity = $this->input->post('overall_total_product_quantity');
            $overall_assigned_product_quantity = $this->input->post('overall_assigned_product_quantity');
            
            
            // New Ends
          
            
                 
            //$this->sales_model->cuttingUnitStatus($cutting_sale_id, $cutting_products, $cutting_products_name, $cutting_username, $cutting_assign_date, $cutting_quantity, $cutting_total_quantity, $cutting_status, $overall_d_date, $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);
            
            $this->sales_model->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);
            
               
            /*echo json_encode($response);
            return 0;*/
            
             $response = array(
                    'status' => 'success',
                    'message' => 'Cutting Unit Assigned Completed'
                    );
            echo json_encode($response);
            return 0;
            
            $this->session->set_flashdata('message', 'Update Successfully...');
                redirect('sales');
            } else {
                /*$this->session->set_flashdata('error', validation_errors());
                redirect('sales');*/
                $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
                 echo json_encode($response);
                return 0;
            }
            echo json_encode($response);
            
    }
    public function stitching_new()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('cutting_sale_id', lang('cutting_sale_id'), 'required');
        
        
        
        
        if ($this->form_validation->run() == true) {
            
            
            // New Starts
            $cutting_sale_id = $this->input->post('cutting_sale_id', true);
            $cutting_status = $this->input->post('cutting_status');
            
            $cutting_products =array();
            $cutting_products_name=array();
            $cutting_username=array();
            $cutting_assign_date=array();
            $cutting_quantity=array();
            $cutting_total_quantity=array();
            $update_work_id = array();
            $new_status=array();
            
            $works_cutting_products=array();
            $works_cutting_products_name=array();
            $works_cutting_username=array();
            $works_cutting_assign_date=array();
            $works_cutting_quantity=array();
            $works_cutting_total_quantity=array();
            
            $overall_total_product_quantity=array();
            $overall_assigned_product_quantity=array();
            
            $cutting_products = $this->input->post('cutting_products');
            $cutting_products_name = $this->input->post('cutting_products_name');
            $cutting_username = $this->input->post('cutting_user_name');
            $cutting_assign_date = $this->input->post('cutting_assign_date');
            $cutting_quantity = $this->input->post('cutting_quantity');
            $cutting_total_quantity = $this->input->post('cutting_total_quantity');
            
            
            $update_work_id = $this->input->post('cutting_update_works_id');
            $new_status = $this->input->post('new_status');
            
            
            $works_cutting_products = $this->input->post('works_cutting_products');
            $works_cutting_products_name = $this->input->post('works_cutting_products_name');
            $works_cutting_username = $this->input->post('works_cutting_username');
            $works_cutting_assign_date = $this->input->post('works_cutting_assign_date');
            $works_cutting_quantity = $this->input->post('works_cutting_quantity');
            $works_cutting_total_quantity = $this->input->post('works_cutting_total_quantity');
            
            
            $overall_total_product_quantity = $this->input->post('overall_total_product_quantity');
            $overall_assigned_product_quantity = $this->input->post('overall_assigned_product_quantity');
            
            
            // New Ends
          
            
           // $this->sales_model->stitchingStatus($cutting_sale_id, $cutting_products, $cutting_products_name, $cutting_username, $cutting_assign_date, $cutting_quantity, $cutting_total_quantity, $cutting_status, $overall_d_date, $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);
             
             
             $this->sales_model->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);
            
            $response = array(
                    'status' => 'success',
                    'message' => 'Stitching Unit Assigned Completed'
                    );
            echo json_encode($response);
            return 0;
             
            $this->session->set_flashdata('message', 'Update Stithcing Successfully...');
                redirect('sales');
            } else {
                /*$this->session->set_flashdata('error', validation_errors());
                redirect('sales');*/
                $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
                 echo json_encode($response);
                return 0;
            }
        echo json_encode($response);
    }
    // New Method Update Measurement Endss Here 
    
    
    
    // New Method Update Measurement Starts Here 
    
    public function ready_to_delivery_new()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('sale_id', lang('sale_id'), 'required');
        
        
        
        
        if ($this->form_validation->run() == true) {
            
            
            
            $sale_id = $this->input->post('sale_id', true);
            $update_work_id = array();
            $products =array();
            $products_name=array();
            $quantity=array();
            $total_quantity=array();
            $ready_or_not_ready=array();
            
            
            $update_work_id = $this->input->post('ready_update_works_id');
            $products = $this->input->post('products');
            $products_name = $this->input->post('products_name');
            $quantity = $this->input->post('quantity');
            $total_quantity = $this->input->post('total_quantity');
            $ready_or_not_ready = $this->input->post('ready_not_ready');
            $measurement_status_insert = $this->input->post('measurement_status_insert');
            $measurement_status_update = $this->input->post('measurement_status_update');
            
            // Update Section (Insert)
            $new_status = array();
            $new_products =array();
            $new_products_name=array();
            $new_quantity=array();
            $new_total_quantity=array();
            $new_ready_or_not_ready=array();
            $new_status = $this->input->post('new_status');
            $new_products = $this->input->post('new_products');
            $new_products_name = $this->input->post('new_products_name');
            $new_quantity = $this->input->post('new_quantity');
            $new_total_quantity = $this->input->post('new_total_quantity');
            $new_ready_or_not_ready = $this->input->post('new_ready_not_ready');
            
            // Total and Assigned Quantity
            $overall_total_product_quantity=array();
            $overall_assigned_product_quantity=array();
            $overall_total_product_quantity = $this->input->post('overall_total_product_quantity');
            $overall_assigned_product_quantity = $this->input->post('overall_assigned_product_quantity');
            
            
            
            $this->sales_model->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);
            
            
            $response = array(
                    'status' => 'success',
                    'message' => 'Ready to Delivery Completed'
                    );
            echo json_encode($response);
            return 0;
            
            $this->session->set_flashdata('message', 'Update Ready to Delivery Successfully...');
                redirect('sales');
            } else {
                /*$this->session->set_flashdata('error', validation_errors());
                redirect('sales');*/
                $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
                echo json_encode($response);
                return 0;
            }
            echo json_encode($response);
    }
    public function delivery_date_new()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('sale_id', lang('sale_id'), 'required');
        
        
        
        
        if ($this->form_validation->run() == true) {
            
            
            
            $sale_id = $this->input->post('sale_id', true);
            $update_work_id = array();
            $products =array();
            $products_name=array();
            $quantity=array();
            $total_quantity=array();
            $delivery_date=array();
            
            $update_work_id = $this->input->post('update_works_id');
            $products = $this->input->post('products');
            $products_name = $this->input->post('products_name');
            $quantity = $this->input->post('quantity');
            $total_quantity = $this->input->post('total_quantity');
            $measurement_status = $this->input->post('measurement_status');
            $delivery_date = $this->input->post('delivery_date');
            
            // Update Section (Insert)
            $new_status = array();
            $new_products =array();
            $new_products_name=array();
            $new_quantity=array();
            $new_total_quantity=array();
            $new_ready_or_not_ready=array();
            $new_status = $this->input->post('new_status');
            $new_products = $this->input->post('new_products');
            $new_products_name = $this->input->post('new_products_name');
            $new_quantity = $this->input->post('new_quantity');
            $new_total_quantity = $this->input->post('new_total_quantity');
            $new_delivery_date = $this->input->post('new_delivery_date');
            
            // Total and Assigned Quantity
            $overall_total_product_quantity=array();
            $overall_assigned_product_quantity=array();
            $overall_total_product_quantity = $this->input->post('overall_total_product_quantity');
            $overall_assigned_product_quantity = $this->input->post('overall_assigned_product_quantity');
            
            
            
            $this->sales_model->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);
             
            $response = array(
                    'status' => 'success',
                    'message' => 'Delivery Date Completed'
                    );
            echo json_encode($response);
            return 0;
            
            $this->session->set_flashdata('message', 'Update Delivery Date Successfully...');
                redirect('sales');
            } else {
                /*$this->session->set_flashdata('error', validation_errors());
                redirect('sales');*/
                $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
                echo json_encode($response);
                return 0;
            }
            echo json_encode($response);
    }
    
    public function overall_delivery_date()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('delivery_date_sale_id', lang('delivery_date_sale_id'), 'required');
        
        
        if ($this->form_validation->run() == true) {
            
            
            
            $sale_id = $this->input->post('delivery_date_sale_id', true);;
            $full_delivery_date = $this->input->post('full_delivery_date');
            $full_delivery_date_status = $this->input->post('full_delivery_date_status');
            
            
            $this->sales_model->overAllDeliveryStatus($sale_id, $full_delivery_date, $full_delivery_date_status);
            
            $response = array(
                    'status' => 'success',
                    'message' => 'Fully Delivery Date Completed'
                    );
            echo json_encode($response);
            return 0;
            
            $this->session->set_flashdata('message', 'Full Products Delivery Date Successfully...');
                redirect('sales');
            } else {
                /*$this->session->set_flashdata('error', validation_errors());
                redirect('sales');*/
                $response = array(
                    'status' => 'error',
                    'message' => 'Error'
                    );
            echo json_encode($response);
            return 0;
            }
            echo json_encode($response);
    }
    
    public function bill_cancel_new()
    {
        if (!$this->Admin) {
            $this->session->set_flashdata('warning', lang('access_denied'));
            redirect('sales');
        }
        $this->form_validation->set_rules('bill_cancel_sale_id', lang('bill_cancel_sale_id'), 'required');
        
        
        if ($this->form_validation->run() == true) {
            
            
            
            $sale_id = $this->input->post('bill_cancel_sale_id', true);;
            $bill_cancel_status = $this->input->post('bill_cancel_status');
            $bill_cancell_done = $this->input->post('bill_cancell_done');
            
            
            $this->sales_model->billCancelStatus($sale_id, $bill_cancel_status, $bill_cancell_done);
             
            $this->session->set_flashdata('message', 'Bill Cancelled Successfully...');
                redirect('sales');
            } else {
                $this->session->set_flashdata('error', validation_errors());
                redirect('sales');
            }
    }
    // New Method Update Measurement Endss Here 
    
    
    
    
    
    // (111111111) ***********************  Latest NEWWWWWWWWWWWWWWWWW Cutting Status Starts Here  ****************************************//
    public function get_all_sale_cutting_items()
        {
            
            $sales_id = $this->input->get('sale_id');
            $date = $this->input->get('date');
            
            $mainsale_datas=$this->sales_model->getSaleMainTable($sales_id);
            $data=$this->sales_model->getSaleIemsBySaleID($sales_id);
            $work_units=$this->sales_model->getSaleWorkUnitsTable($sales_id);
            $sale_works_data=$this->sales_model->getSaleIemsBySaleIDWithWorksTable($sales_id);
            
            $user_data = $this->sales_model->getCuttingUsers();
            
            
            $content='<div class="row">
                                <div class="col-md-3">
                                    <label for="products">Products</label>
                                </div>
                                <div class="col-md-3">
                                    <label for="products">Users</label>
                                </div>
                                <div class="col-md-2">
                                    <label for="products">Qty</label>
                                </div>
                                <div class="col-md-3">
                                    <label for="products">Assign Date</label>
                                </div>
                                <div class="col-md-1">
                                    <label for="products">Actions</label>
                                </div>
                      </div>';
                      
                      
                      
            if($mainsale_datas){
                foreach($mainsale_datas as $salemain){
                    
                    if($salemain->cutting_status==1){
                        
                        
                        /********* Cutting Status Starts ************/
                        $content.='<center><h6>New Cutting Status Not Assign Working</h6></center>';
                        
                        
                        if($data){
                            $content.='<input type="hidden" value="'.$sales_id.'" id="cutting_sale_ids" name="cutting_sale_id" />
                                       <input type="hidden" value="2" id="cutting_status" name="cutting_status" />';
                                       
                                foreach($data as $d){
                                
                                
                                if($d->product_type=="service" && $d->cutting_status=="1"){
                                    
                                    
                                    $user_content='<option value="">Select User</option>';
                                    if($user_data){
                                        foreach($user_data as $user){
                                            error_log("User Categories: ".$user->categories);
                                            error_log("Pant or Shirt ID: ".$user->categories);
                                            if($user->categories=="Pant" && $d->pant_or_shirt==1 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==1)
                                            {
                                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            if($user->categories=="Shirt" && $d->pant_or_shirt==2 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==2)
                                            {
                                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                                
                                        }
                                    }else{
                                        $user_content.='<option value="">No Data Available</option>';
                                    }
                                    
                                    
                                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                                    
                                    
                                   
                                    
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                    <b>Total Quantity: <span id="total_qty_'.$d->product_id.'"></span></b>
                                                    <input type="text" name="overall_total_product_quantity[]"  id="total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                                
                                                </div>';
                                                
                                    if($d->remaing_cutting_qty==""){$d->remaing_cutting_qty=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                                
                                                 <b id="bal_total_qty_'.$d->product_id.'">Remaining Quantity: <span id="balance_qty_'.$d->product_id.'">0</span></b>
                                                 <input type="text" name="overall_assigned_product_quantity[]" id="assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                                        
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                                    
                                     $content.='<span id="expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                  

                                    $content.='</div>';
                                    
                                    
                                   
                                    
                                    
                                    $content.='<div id="expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
            
                                    
                                    $content.='</div>';
                                    
                                    
                                    $content.='<script>
                                                $("#expand_sale_items_'.$d->product_id.'").on("click", function() {
                                                
                                                    html=`<div class="rows"><div class="col-md-3"><select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option>        </select><input type="hidden" name="cutting_products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"></div><div class="col-md-3"><select id="user_name_'.$d->id.'" name="cutting_user_name[]" class="form-control tip select2" style="width:100%;" required>'.$user_content.'</select></div><div class="col-md-2"><input type="number" name="cutting_quantity[]" id="qty_'.$d->id.'" value="" min="1" max="'.number_format("$d->quantity").'" class="form-control tip qqty_'.$d->id.' quantity" style="width:100%;" required /><input type="hidden" id="total" name="cutting_total_quantity[]" value="'.number_format("$d->quantity").'" min="1" max="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" /></div><div class="col-md-3"><input type="date" id="date_'.$d->id.'" value="'.$d->cutting_assign_date.'" name="cutting_assign_date[]" style="padding: 8px;" class="form-control tip today_date" required></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="expand_remove_'.$d->product_id.'"  class="btn btn-danger btn-xs expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                                    $("#expand_conteent_'.$d->product_id.'").append(html);
                                                    
                                                });
                                              </script>';
                                              
                                    $content.='<script>
                                                    $(document).ready(function(){
                                                        $("#total_qty_'.$d->product_id.'").text('.number_format("$d->quantity").'); 
                                                    });
                                                    
                                                    
                                                    
                                            $(function() {
                                            
                                                    $(document).ready(function(){
                                                        grand_total();
                                                    });
                                                    $(document).on("click", ".expand_remove_'.$d->product_id.'", function() {
                                                        $(this).closest(".rows").remove();
                                                        grand_total();
                                                    });
                                                    $("body").on("keyup",".qqty_'.$d->id.'",function(){
                                                      var quantity=Number($(this).val());
                                                      grand_total();
                                                          
                                                    });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".qqty_'.$d->id.'").each(function(){
                                                        tot+=Number($(this).val());
                                                  });
                                                                var total = $("#total_product_quantity_'.$d->product_id.'").val();
                                                                console.log("Total:"+tot);
                                                                grand_balance=total-tot;
                                                                $("#balance_qty_'.$d->product_id.'").text(grand_balance);
                                                                $("#baal_total_qty_'.$d->product_id.'").val(grand_balance);
                                                                $("#assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                                
                                                                $("#qtty_'.$d->id.'").val(grand_balance);
                                                                if($("#balance_qty_'.$d->product_id.'").text() < 0){ 
                                                                     
                                                                     $("#bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                                     $("#cutting_submit").prop("disabled", true);
                                                                }else{
                                                                    $("#bal_total_qty_'.$d->product_id.'").css({"color": "blue"});
                                                                    $("#cutting_submit").prop("disabled", false);
                                                                }
                                                                
                                                                if($("#balance_qty_'.$d->product_id.'").text() == 0){ 
                                                                     $("#bal_total_qty_'.$d->product_id.'").css({"color" : "black"});
                                                                }
                                                  
                                            }
                                        });
                                        
                                                </script><style>
                                                                .redBackground {
                                                                   background-color: red;
                                                                 }
                                                                 </style>';  
                                }
                                }
                                
                            $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="cutting_submit" class="btn btn-success" value="Submit"></div>';
                                    
                                    
                        }            
                        
                        
                        /********* Cutting Status Endss ************/
                        
                    }else if($salemain->cutting_status==2){
                        
                        /********* Cutting Status Partial Starts ************/
                        $content.='<center><h6>Cutting Status Partial Assign Working</h6></center>';
                        
                        if($data){
                            
                            $content.='<input type="hidden" value="'.$sales_id.'" id="cutting_sale_ids" name="cutting_sale_id" />
                                       <input type="hidden" value="22" id="cutting_status" name="cutting_status" />';
                                       
                                foreach($data as $d){
                                
                                
                                
                                if($d->product_type=="service" && $d->cutting_status=="0" || $d->product_type=="service" && $d->cutting_status=="1"){
                                    
                                    
                                     $new_user_content='<option value="">Select Users</option>';
                                    $user_content='<option value="">Select Users</option>';
                                    if($user_data){
                                        foreach($user_data as $user){
                                            
                                            $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';

                                            if($user->categories=="Pant" && $d->pant_or_shirt==1 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==1)
                                            {
                                                $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            if($user->categories=="Shirt" && $d->pant_or_shirt==2 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==2)
                                            {
                                                $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            
                                        }
                                    }else{
                                        $user_content.='<option value="">No Data Available</option>';
                                        $new_user_content.='<option value="">No Data Available</option>';
                                    }
                                    
                                    
                                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #a5eda5;">';
                                    
                                    
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                 <b>Total Quantity : <span id="total_qty_'.$d->product_id.'">'.number_format("$d->quantity").'</span></b>
                                                 <input type="text" name="overall_total_product_quantity[]"  id="total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                                 
                                                </div>';
                                                
                                    if($d->remaing_cutting_qty==""){$d->remaing_cutting_qty=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                                
                                                 <b id="bal_total_qty_'.$d->product_id.'"><b>Remaining Quantity : <span id="balance_qty_'.$d->product_id.'">'.$d->remaing_cutting_qty.'</span></b></b>
                                                 <input type="text" name="overall_assigned_product_quantity[]" id="assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                                 
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                                    
                                     $content.='<span id="expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                  

                                    $content.='</div>';
                                    
                                    
                                    // New One Starts Here 
                                    foreach($work_units as $works){
                                        $user_content='<option value="">Select Users</option>';
                                        $new_user_content='<option value="">Select Users</option>';
                                        if($user_data){
                                            foreach($user_data as $user){
                                                /*$user_content.='<option value="'.$user->id.'" '.(($works->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';*/
                                            
                                            
                                            $user_content.='<option value="'.$user->id.'" '.(($works->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';

                                            if($user->categories=="Pant" && $d->pant_or_shirt==1 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==1)
                                            {
                                                $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            if($user->categories=="Shirt" && $d->pant_or_shirt==2 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==2)
                                            {
                                                $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            
                                            }
                                        }else{
                                            $user_content.='<option value="">No Data Available</option>';
                                            $new_user_content.='<option value="">No Data Available</option>';
                                        }
                                    if($works->type=="1" && $works->product_id==$d->product_id){
                                    
                                    
                                    
                                    $content.='<div id="xyz_'.$works->id.'">   
                                               <div class="col-md-3" style="padding-top: 10px;">
                                               <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;">
                                               <option value="'.$works->product_id.'">'.$works->product_name.'</option>
                                               </select>
                                                </div>';
                                      
                                    
                                    $content.='<input type="hidden" name="cutting_products_name[]" value="'.$works->product_name.'" class="form-control tip" style="width:100%;">
                                               <input type="hidden" name="cutting_update_works_id[]" value="'.$works->id.'" class="form-control tip" style="width:100%;">';
                                    
                                    
                                    $content.='<div class="col-md-3" style="padding-top: 10px;">
                                               <select id="user_name_'.$works->id.'" name="cutting_user_name[]" class="form-control select2" style="width:100%;" required>
                                               '.$user_content.'
                                               </select>
                                               </div>';
                
                                    
                                    $content.='<div class="col-md-2" style="padding-top: 10px;">
                                               <input type="number" id="qty_'.$d->product_id.'" name="cutting_quantity[]" value="'.$works->quantity.'" class="form-control tip qqty_'.$d->product_id.' quantity" style="width:100%;" required/>
                                              </div>';
                                              
                                    $content.='<input type="hidden" id="bal_'.$d->product_id.'"  value="" class="form-control tip" style="width:100%;" />
                                                <input type="hidden" id="total" name="cutting_total_quantity[]" value="'.number_format("$works->quantity").'" class="form-control tip" style="width:100%;" />';
                                    
                                    
                                    $content.='<div class="col-md-3" style="padding-top: 10px;">
                                               <input type="date" id="date_'.$works->id.'" value="'.$works->cutting_assign_date.'" name="cutting_assign_date[]" style="padding: 8px;" class="form-control tip today_date" required>
                                               </div>';
                                    
                                    
                                    $content.='<div class="col-md-1" style="text-align: center; padding: 10px 0px 0px 5px;">';
                                    
                                    $content.='<a onClick="deleteAssignedID('.$works->id.','.$d->product_id.')" class="tip btn btn-danger btn-xs" style="padding: 5px 10px 5px 10px;"><i class="fa fa-trash-o"></i></a>';
                                     $content.='<input type="hidden" id="work_sale_id" value="'.$works->sale_id.'" name="work_sale_id[]" class="form-control work_sale_id" style="width:100%;">
                                                <input type="hidden" id="work_product_id" value="'.$works->product_id.'" name="work_product_id[]" class="form-control work_product_id" style="width:100%;">';
                                    $content.='</div>';
                                    $content.='</div>';
                                    
                                    
                                    
                                    }
                                        
                                    }
                                    // New One Ends Here
                                    
                                    $content.='<div id="expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
                                   
                                    $content.='<div>';
                                    
                                    $content.='<script>
                                                $("#expand_sale_items_'.$d->product_id.'").on("click", function() {
                                                
                                                    html=`<div class="rows"><div class="col-md-3"><select id="products" name="works_cutting_products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option>        </select><input type="hidden" name="works_cutting_products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"></div><div class="col-md-3"><input name="new_status[]" type="hidden" value="0"><select id="user_name_'.$works->id.'" name="works_cutting_username[]" class="form-control tip" style="width:100%;" required>'.$new_user_content.'</select></div><div class="col-md-2"><input type="number" name="works_cutting_quantity[]" id="qty_'.$works->id.'" value="" class="form-control tip qqty_'.$d->product_id.' quantity" style="width:100%;" required/><input type="hidden" id="total" name="works_cutting_total_quantity[]" value="'.number_format("$works->quantity").'" min="1"  class="form-control tip" style="width:100%;" /></div><div class="col-md-3"><input type="date" id="date_'.$d->id.'" value="" name="works_cutting_assign_date[]" style="padding: 8px;" class="form-control tip today_date" required></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="expand_remove_'.$works->product_id.'"  class="btn btn-danger btn-xs expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                                    $("#expand_conteent_'.$d->product_id.'").append(html);
                                                    
                                                    
                                                });
                                              </script>';
                                    
                                    
                                    
                                    
                                    $content.='</div></div>';
                                    
                                    
                                    
                                   $content.='<script> 
                                                    
                                                
                                                $(function() {
                                                
                                                
                                                    $(document).on("click", ".expand_remove_'.$d->product_id.'", function() {
                                                        $(this).closest(".rows").remove();
                                                        grand_total();
                                                    });
                                                    $(document).ready(function(){
                                                        grand_total();
                                                    });
                                                    $("body").on("keyup",".qqty_'.$d->product_id.'",function(){
                                                      var quantity=Number($(this).val());
                                                      console.log("ASSIGNED QUANTITY"+quantity);
                                                      grand_total();
                                                    });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".qqty_'.$d->product_id.'").each(function(){
                                                    tot+=Number($(this).val());
                                                  });
                                                  var total = $("#total_product_quantity_'.$d->product_id.'").val();
                                                    console.log("Total Now Quantity:"+tot);
                                                    grand_balance=total-tot;
                                                    
                                                    console.log(grand_balance);
                                                    $("#balance_qty_'.$d->product_id.'").text(grand_balance);
                                                    $("#assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                    
                                                    $("#qtty_'.$d->product_id.'").val(grand_balance);
                                                    if($("#balance_qty_'.$d->product_id.'").text() < 0){
                                                         $("#bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                         $("#cutting_update").prop("disabled", true);
                                                    }else{
                                                        $("#bal_total_qty_'.$d->product_id.'").css({"color":"blue"});
                                                        $("#cutting_update").prop("disabled", false);
                                                    }
                                                    if($("#balance_qty_'.$d->product_id.'").text() == 0){
                                                         $("#bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                    }
                                                  
                                            }
                                        });
                                        
                                    </script>';
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                              
                                      
                                }
                                
                                    
                                    
                                    
                                }
                                
                                   $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="cutting_update" class="btn btn-success" value="Update"></div>';
                                     
                        }   
                        
                        
                        
                        
                        
                        
                    }else{
                        $content.='<center><h6>Cutting Status Fully Assign Working</h6></center>';
                    }
                }
            }        
                      
           
                
       
                        
                        
                        
                            
                            
                  /*$content.= $button;*/
           else{
                $content.='No Data Available';
            }
            echo $content;
            
        }
    // (111111111) ***********************  Latest NEWWWWWWWWWWWWW Cutting Status Endss Here **************************************** // 
    
    
    
    
    
    
    
    // (222222222) ***********************  Latest NEWWWWWWWWWWWWWWWWW Stitching Status Starts Here  ****************************************//
    public function get_all_sale_stitching_items()
        {
            
            $sales_id = $this->input->get('sale_id');
            $date = $this->input->get('date');
            
            $mainsale_datas=$this->sales_model->getSaleMainTable($sales_id);
            $data=$this->sales_model->getSaleIemsBySaleID($sales_id);
            $work_units=$this->sales_model->getSaleWorkUnitsTable($sales_id);
            $sale_works_data=$this->sales_model->getSaleIemsBySaleIDWithWorksTable($sales_id);
            
            $user_data = $this->sales_model->getStitchingUsers();
            
            
            $content='<div class="row">
                                <div class="col-md-3">
                                    <label for="products">Products</label>
                                </div>
                                <div class="col-md-3">
                                    <label for="products">Users</label>
                                </div>
                                <div class="col-md-2">
                                    <label for="products">Qty</label>
                                </div>
                                <div class="col-md-3">
                                    <label for="products">Assign Date</label>
                                </div>
                                <div class="col-md-1">
                                    <label for="products">Actions</label>
                                </div>
                      </div>';
                      
                      
                      
            if($mainsale_datas){
                foreach($mainsale_datas as $salemain){
                    
                    if($salemain->stitching_status==1){
                        
                        
                        /********* Cutting Status Starts ************/
                        $content.='<center><h6>NEWW Stitching Status Not Assign Working</h6></center>';
                        
                        
                        if($data){
                            
                             $content.='<input type="hidden" value="'.$sales_id.'" id="cutting_sale_ids" name="cutting_sale_id" />
                                       <input type="hidden" value="4" id="cutting_status" name="cutting_status" />';
                                       
                                foreach($data as $d){
                                
                                
                                if($d->product_type=="service" && $d->stitching_status=="1"){
                                    
                                    
                                    $user_content='<option value="">Select User</option>';
                                    if($user_data){
                                        foreach($user_data as $user){
                                            
                                            if($user->categories=="Pant" && $d->pant_or_shirt==1 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==1)
                                            {
                                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            if($user->categories=="Shirt" && $d->pant_or_shirt==2 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==2)
                                            {
                                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                        }
                                    }else{
                                        $user_content.='<option value="">No Data Available</option>';
                                    }
                                    
                                    
                                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                                    
                                  
                                      
                                    
                                    
                                    
                                     
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                   <b>Total Quantity: <span id="s_total_qty_'.$d->product_id.'"></span></b>
                                                   <input type="text" name="overall_total_product_quantity[]"  id="s_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                                
                                                </div>';
                                                
                                    if($d->remaing_stitching_qty==""){$d->remaing_stitching_qty=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                                
                                                  <b id="s_bal_total_qty_'.$d->product_id.'">Remaining Quantity: <span id="s_balance_qty_'.$d->product_id.'">'.$d->remaing_stitching_qty.'</span></b>
                                                  <input type="text" name="overall_assigned_product_quantity[]" id="s_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                                        
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                
                                         $content.='<span id="s_expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                    

                                    $content.='</div>';
                                    
                                    
                                    
                                    
                                    $content.='<div id="s_expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
                                    /*$content.='<div class="row">
                                                    <div class="col-md-6" style="padding-left: 50px;">
                                                        <b>Total Quantity: <span id="s_total_qty_'.$d->product_id.'"></span></b>
                                                        <input type="text" name="overall_total_product_quantity[]"  id="s_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                                    </div>
                                                    <div class="col-md-6" style="padding-left: 50px;">
                                                        <b id="s_bal_total_qty_'.$d->product_id.'">Remaining Quantity: <span id="s_balance_qty_'.$d->product_id.'">0</span></b>
                                                        <input type="text" name="overall_assigned_product_quantity[]" id="s_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                                    </div>
                                                </div>';*/
                                    
                                    $content.='</div>';
                                    
                                    
                                    $content.='<script>
                                                $("#s_expand_sale_items_'.$d->product_id.'").on("click", function() {
                                                
                                                    html=`<div class="rows"><div class="col-md-3"><select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option>        </select><input type="hidden" name="cutting_products_name[]" value="'.$d->product_name.'" class="form-control" style="width:100%;"></div><div class="col-md-3"><select id="user_name_'.$d->id.'" name="cutting_user_name[]" class="form-control select2" style="width:100%;" required>'.$user_content.'</select></div><div class="col-md-2"><input type="number" name="cutting_quantity[]" id="s_qty_'.$d->id.'" value="" min="1" max="'.number_format("$d->quantity").'" class="form-control s_qqty_'.$d->id.' s_quantity" style="width:100%;" required /><input type="hidden" id="total" name="cutting_total_quantity[]" value="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" /></div><div class="col-md-3"><input type="date" id="date_'.$d->id.'" value="'.$d->cutting_assign_date.'" name="cutting_assign_date[]" style="padding: 8px;" class="form-control tip today_date" required></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="expand_remove_'.$d->product_id.'"  class="btn btn-danger btn-xs s_expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                                    $("#s_expand_conteent_'.$d->product_id.'").append(html);
                                                    /*$(".select2").select2();*/
                                                    
                                                });
                                              </script>';
                                              
                                    $content.='<script>
                                                    $(document).ready(function(){
                                                        $("#s_total_qty_'.$d->product_id.'").text('.number_format("$d->quantity").'); 
                                                    });
                                                    
                                                    
                                                    
                                            $(function() {
                                            
                                                    $(document).ready(function(){
                                                        grand_total();
                                                    });
                                                    $(document).on("click", ".s_expand_remove_'.$d->product_id.'", function() {
                                                        $(this).closest(".rows").remove();
                                                        grand_total();
                                                    });
                                                    $("body").on("keyup",".s_qqty_'.$d->id.'",function(){
                                                      var quantity=Number($(this).val());
                                                      grand_total();
                                                          
                                                    });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".s_qqty_'.$d->id.'").each(function(){
                                                        tot+=Number($(this).val());
                                                  });
                                                                var total = $("#s_total_product_quantity_'.$d->product_id.'").val();
                                                                console.log("Stitching Total:"+tot);
                                                                grand_balance=total-tot;
                                                                $("#s_balance_qty_'.$d->product_id.'").text(grand_balance);
                                                                $("#s_baal_total_qty_'.$d->product_id.'").val(grand_balance);
                                                                $("#s_assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                                
                                                                $("#s_qtty_'.$d->id.'").val(grand_balance);
                                                                if($("#s_balance_qty_'.$d->product_id.'").text() < 0){
                                                                     $("#s_bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                                     $("#stithcing_submit").prop("disabled", true);
                                                                }else{
                                                                    $("#s_bal_total_qty_'.$d->product_id.'").css({"color":"blue"});
                                                                    $("#stithcing_submit").prop("disabled", false);
                                                                }
                                                                if($("#s_balance_qty_'.$d->product_id.'").text() == 0){
                                                                     $("#s_bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                                }
                                                  
                                            }
                                        });
                                        
                                                </script>';  
                                }
                                }
                                
                            $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="stithcing_submit" class="btn btn-success" value="Submit"></div>';
                                    
                                    
                        }  
                        
                        
                        /********* Cutting Status Endss ************/
                        
                    }else if($salemain->stitching_status==2){
                        
                        /********* Cutting Status Partial Starts ************/
                        $content.='<center><h6>Stitching Status Partial Assign Working</h6></center>';
                        
                        if($data){
                            
                           $content.='<input type="hidden" value="'.$sales_id.'" id="cutting_sale_ids" name="cutting_sale_id" />
                                       <input type="hidden" value="44" id="cutting_status" name="cutting_status" />';
                                       
                                foreach($data as $d){
                                
                                
                                if($d->product_type=="service" && $d->stitching_status=="0" || $d->product_type=="service" && $d->stitching_status=="1"){
                                    
                                    
                                    $user_content='<option value="">Select User</option>';
                                    if($user_data){
                                        foreach($user_data as $user){
                                            
                                            if($user->categories=="Pant" && $d->pant_or_shirt==1 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==1)
                                            {
                                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            if($user->categories=="Shirt" && $d->pant_or_shirt==2 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==2)
                                            {
                                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            
                                        }
                                    }else{
                                        $user_content.='<option value="">No Data Available</option>';
                                    }
                                    
                                    
                                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #a5eda5;">';
                                    
                                    
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                 <b>Total Quantity : <span id="s_total_qty_'.$d->product_id.'">'.number_format("$d->quantity").'</span></b>
                                                 <input type="text" name="overall_total_product_quantity[]"  id="s_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                                 
                                                </div>';
                                                
                                    if($d->remaing_stitching_qty==""){$d->remaing_stitching_qty=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                                
                                                 <b id="s_bal_total_qty_'.$d->product_id.'">
                                                 Remaining Quantity : <span id="s_balance_qty_'.$d->product_id.'">'.$d->remaing_stitching_qty.'</span></b>
                                                 <input type="text" name="overall_assigned_product_quantity[]" id="s_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                                 
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 5px 0px 0px 5px;">';
                                    
                                     $content.='<span id="s_expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                  

                                    $content.='</div>';
                                    
                                   
                                    
                                    // New One Starts Here 
                                    foreach($work_units as $works){
                                        $user_content='<option value="0">Select User</option>';
                                        $new_user_content='<option value="0">Select User</option>';
                                        if($user_data){
                                            foreach($user_data as $user){
                                                
                                            $user_content.='<option value="'.$user->id.'" '.(($works->stitching_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';

                                            if($user->categories=="Pant" && $d->pant_or_shirt==1 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==1)
                                            {
                                            $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';                                            }
                                            if($user->categories=="Shirt" && $d->pant_or_shirt==2 || $user->categories=="Pant and Shirt" && $d->pant_or_shirt==2)
                                            {
                                            $new_user_content.='<option value="'.$user->id.'">'.$user->first_name. "" . $user->last_name.'</option>';
                                            }
                                            
                                                
                                            }
                                        }else{
                                            $user_content.='<option value="">No Data Available</option>';
                                            $new_user_content.='<option value="">No Data Available</option>';
                                        }
                                    if($works->type=="2" && $works->product_id==$d->product_id){
                                    
                                    
                                    
                                    $content.='<div id="xyz_'.$works->id.'">   
                                               <div class="col-md-3" style="padding-top: 10px;">
                                               <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;">
                                               <option value="'.$works->product_id.'">'.$works->product_name.'</option>
                                               </select>
                                                </div>';
                                      
                                    
                                    $content.='<input type="hidden" name="cutting_products_name[]" value="'.$works->product_name.'" class="form-control tip" style="width:100%;">
                                               <input type="hidden" name="cutting_update_works_id[]" value="'.$works->id.'" class="form-control tip" style="width:100%;">';
                                    
                                    
                                    $content.='<div class="col-md-3" style="padding-top: 10px;">
                                               <select id="user_name_'.$works->id.'" name="cutting_user_name[]" class="form-control tip" style="width:100%;" required>
                                               '.$user_content.'
                                               </select>
                                               </div>';
                
                                    
                                    $content.='<div class="col-md-2" style="padding-top: 10px;">
                                               <input type="number" id="s_qty_'.$d->product_id.'" name="cutting_quantity[]" value="'.$works->quantity.'" class="form-control tip s_qqty_'.$d->product_id.' s_quantity" style="width:100%;" required/>
                                              </div>';
                                              
                                    $content.='<input type="hidden" id="s_bal_'.$d->product_id.'"  value="" class="form-control tip" style="width:100%;" />
                                                <input type="hidden" id="s_total" name="cutting_total_quantity[]" value="'.number_format("$works->quantity").'" class="form-control tip" style="width:100%;" />';
                                    
                                    
                                    $content.='<div class="col-md-3" style="padding-top: 10px;">
                                               <input type="date" id="date_'.$works->id.'" value="'.$works->stitching_assign_date.'" name="cutting_assign_date[]" style="padding: 8px;" class="form-control tip today_date" required>
                                               </div>';
                                    
                                    
                                    $content.='<div class="col-md-1" style="text-align: center; padding: 10px 0px 0px 5px;">';
                                    
                                    $content.='<a onClick="deleteStitchingAssignedID('.$works->id.','.$d->product_id.')" class="tip btn btn-danger btn-xs" style="padding: 5px 10px 5px 10px;"><i class="fa fa-trash-o"></i></a>';
                                     $content.='<input type="hidden" id="work_sale_id" value="'.$works->sale_id.'" name="work_sale_id[]" class="form-control work_sale_id" style="width:100%;">
                                                <input type="hidden" id="work_product_id" value="'.$works->product_id.'" name="work_product_id[]" class="form-control work_product_id" style="width:100%;">';
                                    $content.='</div>';
                                    $content.='</div>';
                                    
                                    
                                    
                                    }
                                        
                                    }
                                    // New One Ends Here
                                    
                                    $content.='<div id="s_expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
                                   
                                    $content.='<div>';
                                    
                                    $content.='<script>
                                                $("#s_expand_sale_items_'.$d->product_id.'").on("click", function() {
                                                
                                                    html=`<div class="rows"><div class="col-md-3"><select id="products" name="works_cutting_products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option>        </select><input type="hidden" name="works_cutting_products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"></div><div class="col-md-3"><input name="new_status[]" type="hidden" value="0"><select id="user_name_'.$works->id.'" name="works_cutting_username[]" class="form-control tip" style="width:100%;" required>'.$new_user_content.'</select></div><div class="col-md-2"><input type="number" name="works_cutting_quantity[]" id="s_qty_'.$works->id.'" value="" class="form-control tip s_qqty_'.$d->product_id.' s_quantity" style="width:100%;" required/><input type="hidden" id="total" name="works_cutting_total_quantity[]" value="'.number_format("$works->quantity").'" min="1"  class="form-control tip" style="width:100%;" /></div><div class="col-md-3"><input type="date" id="date_'.$d->id.'" value="" name="works_cutting_assign_date[]" style="padding: 8px;" class="form-control tip today_date" required></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="s_expand_remove_'.$works->product_id.'"  class="btn btn-danger btn-xs s_expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                                    $("#s_expand_conteent_'.$d->product_id.'").append(html);
                                                    
                                                    
                                                });
                                              </script>';
                                    
                                    
                                    
                                    
                                    $content.='</div></div>';
                                    
                                    
                                    
                                   $content.='<script> 
                                                    
                                                
                                                $(function() {
                                                    $(document).on("click", ".s_expand_remove_'.$d->product_id.'", function() {
                                                        $(this).closest(".rows").remove();
                                                        grand_total();
                                                    });
                                                    $(document).ready(function(){
                                                        grand_total();
                                                    });
                                                    $("body").on("keyup",".s_qqty_'.$d->product_id.'",function(){
                                                      var quantity=Number($(this).val());
                                                      console.log("ASSIGNED QUANTITY"+quantity);
                                                      grand_total();
                                                    });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".s_qqty_'.$d->product_id.'").each(function(){
                                                    tot+=Number($(this).val());
                                                  });
                                                  var total = $("#s_total_product_quantity_'.$d->product_id.'").val();
                                                    console.log("Total Now Quantity:"+tot);
                                                    grand_balance=total-tot;
                                                    
                                                    console.log(grand_balance);
                                                    $("#s_balance_qty_'.$d->product_id.'").text(grand_balance);
                                                    $("#s_assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                    
                                                    $("#s_qtty_'.$d->product_id.'").val(grand_balance);
                                                    
                                                    
                                                    if($("#s_balance_qty_'.$d->product_id.'").text() < 0){
                                                         $("#s_bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                         $("#stitching_update").prop("disabled", true);
                                                    }else{
                                                        $("#s_bal_total_qty_'.$d->product_id.'").css({"color":"blue"});
                                                        $("#stitching_update").prop("disabled", false);
                                                    }
                                                    
                                                    if($("#s_balance_qty_'.$d->product_id.'").text() == 0){
                                                        $("#s_bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                    }
                                            }
                                        });
                                        
                                    </script>';
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                              
                                      
                                }
                                
                                    
                                    
                                    
                                }
                                
                                   $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="stitching_update" class="btn btn-success" value="Update"></div>';
                                     
                        }   
                        
                        
                        
                        
                        
                        /********* Cutting Status Partial Endss ************/
                        
                    }else{
                        $content.='<center><h6>Cutting Status Fully Assign Working</h6></center>';
                    }
                }
            }        
                      
           
                
       
                        
                        
                        
                            
                            
                  /*$content.= $button;*/
           else{
                $content.='No Data Available';
            }
            echo $content;
            
        }
    // (222222222) ***********************  Latest NEWWWWWWWWWWWWW Stitching Status Endss Here **************************************** // 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   
   
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  
    
    
    
    
    
     // (3333333333) ***********************  Latest NEWWWWWW  Ready to Delievry Startsss Here **************************************** // 
        public function get_all_ready_items()
        {
            
            
            $sales_id = $this->input->get('sale_id');
            $date = $this->input->get('date');
            
            $mainsale_datas=$this->sales_model->getSaleMainTable($sales_id);
            $data=$this->sales_model->getSaleIemsBySaleID($sales_id);
            $work_units=$this->sales_model->getSaleWorkUnitsTable($sales_id);
            
            $user_data = $this->sales_model->getAllStaff();
            
            $content='<div class="row">
                                <div class="col-md-4">
                                    <label for="products">Products</label>
                                </div>
                                <div class="col-md-3">
                                    <label for="products">Qty</label>
                                </div>
                                <div class="col-md-4">
                                    <label for="products">Status</label>
                                </div>
                                <div class="col-md-1">
                                    <label for="products">Actions</label>
                                </div>
                      </div>';
                      
             if($mainsale_datas){
                foreach($mainsale_datas as $salemain){
                    
                    if($salemain->ready_to_delivery_status==1){
                        
                        
                        /********* Ready to Delievry Status Starts ************/
                        $content.='<center><h6>NEWW Ready to Delievry Status Not Assign Working</h6></center>';
                        
                        
                        if($data){
                            $content.='<input type="hidden" value="'.$sales_id.'" id="ready_to_delivery_sale_ids" name="sale_id" />
                                       <input type="hidden" value="6" id="measurement_status" name="measurement_status_insert" />';
                                       
                                foreach($data as $d){
                                
                                
                                if($d->product_type=="service" && $d->ready_products_status=="1"){
                                    
                                    
                                     $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                        
                        
                        
                                     
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                   <b>Total Quantity: <span id="r_total_qty_'.$d->product_id.'"></span></b>
                                            <input type="text" name="overall_total_product_quantity[]"  id="r_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                            
                                                </div>';
                                                
                                    if($d->remaining_ready_to_delivery_qty==""){$d->remaining_ready_to_delivery_qty=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                     
                                                 <b id="r_bal_total_qty_'.$d->product_id.'">
                                            Remaining Quantity: <span id="r_balance_qty_'.$d->product_id.'">'.$d->remaining_ready_to_delivery_qty.'</span></b>
                                            <input type="text" name="overall_assigned_product_quantity[]" id="r_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                            
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                
                        $content.='<span id="r_expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                    

                                    $content.='</div>';
                                    
                        
                        
                        
                        
                        $content.='<div id="r_expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                        
                        
                        /*$content.='<div class="row">
                                        <div class="col-md-6" style="padding-left: 50px;">
                                            <b>Total Quantity: <span id="r_total_qty_'.$d->product_id.'"></span></b>
                                            <input type="text" name="overall_total_product_quantity[]"  id="r_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                        </div>
                                        <div class="col-md-6" style="padding-left: 50px;">
                                            <b id="r_bal_total_qty_'.$d->product_id.'">
                                            Remaining Quantity: <span id="r_balance_qty_'.$d->product_id.'">0</span></b>
                                            <input type="text" name="overall_assigned_product_quantity[]" id="r_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                        </div>
                                    </div>';*/
                        
                        $content.='</div>';
                        
                        // $button='<div style="float: right; padding-top: 30px;"><input type="submit" id="ready_to_delivery_submit" class="btn btn-success" value="Submit"></div>';
                         
                         
                       $content.='<script>
                                        $(document).ready(function(){
                                            $("#r_total_qty_'.$d->product_id.'").text('.number_format("$d->quantity").');  
                                        });
                                        
                                    $("#r_expand_sale_items_'.$d->product_id.'").on("click", function() {
                                        html=`<div class="rows"><div class="col-md-4"><select id="products" name="products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option></select></div><input type="hidden" name="products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"><div class="col-md-3"><input type="number" id="r_qty_'.$d->id.'" name="quantity[]" value="" min="1" max="'.number_format("$d->quantity").'" class="form-control tip r_qqty_'.$d->id.' quantity" style="width:100%;" /><input type="hidden" id="total" name="total_quantity[]" value="'.number_format("$d->quantity").'" min="1" max="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" /></div><div class="col-md-4"><select id="ready_not_ready" name="ready_not_ready[]" class="form-control tip" style="width:100%;"><option value="ready" '.(($d->ready_or_not_ready=="ready")?'selected="selected"':"").'>Ready</option><option value="not_ready" '.(($d->ready_or_not_ready=="not_ready")?'selected="selected"':"").'>Not Ready</option></select></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="vexpand_remove_'.$d->product_id.'"  class="btn btn-danger btn-xs r_expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                        $("#r_expand_conteent_'.$d->product_id.'").append(html);
                                    });
                                        $(function() {
                                            $(document).on("click", ".r_expand_remove_'.$d->product_id.'", function() {
                                                $(this).closest(".rows").remove();
                                                grand_total();
                                            });
                                            $("body").on("keyup",".r_qqty_'.$d->id.'",function(){
                                              var quantity=Number($(this).val());
                                              grand_total();
                                                  
                                            });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".r_qqty_'.$d->id.'").each(function(){
                                                    tot+=Number($(this).val());
                                                  });
                                                    var total = $("#r_total_product_quantity_'.$d->product_id.'").val();
                                                    console.log("Total:"+tot);
                                                    grand_balance=total-tot;
                                                    $("#r_balance_qty_'.$d->product_id.'").text(grand_balance);
                                                    $("#r_not_assign_qty_'.$d->id.'").val(grand_balance);
                                                    $("#r_assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                    
                                                    if($("#r_balance_qty_'.$d->product_id.'").text() < 0){
                                                         $("#r_bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                         $("#ready_to_delivery_submit").prop("disabled", true);
                                                    }else{
                                                        $("#r_bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                        $("#ready_to_delivery_submit").prop("disabled", false);
                                                    }
                                                  
                                            }
                                        });
                                    </script>';
                                }
                                }
                                
                            $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="ready_to_delivery_submit" class="btn btn-success" value="Submit"></div>';
                                    
                                    
                        }            
                        
                        
                        /********* Cutting Status Endss ************/
                        
                    }else if($salemain->ready_to_delivery_status==2){
                        
                        /********* Ready to Delievry Partial Starts ************/
                        $content.='<center><h6>Ready to Delievry Status Partial Assign Working</h6></center>';
                        
                        
                        /********* NEW METHOD READY TO DELIVERY STARTS HERE ***************/
                        if($data){
                            
                           $content.='<input type="hidden" value="'.$sales_id.'" id="ready_to_delivery_sale_ids" name="sale_id" />
                                        <input type="hidden" value="66" id="measurement_status" name="measurement_status_update" />';
                                       
                                foreach($data as $d){
                                
                                
                                if($d->product_type=="service" && $d->ready_products_status=="0" || $d->product_type=="service" && $d->ready_products_status=="1"){
                                    
                                    
                                    
                                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #a5eda5;">';
                                    
                                    
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                 <b>Total Quantity : <span id="r_total_qty_'.$d->product_id.'">'.number_format("$d->quantity").'</span></b>
                                                 <input type="text" name="overall_total_product_quantity[]"  id="r_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                                 
                                                </div>';
                                                
                                    if($d->remaining_ready_to_delivery_qty==""){$d->remaining_ready_to_delivery_qty=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                                
                                                 <b id="r_bal_total_qty_'.$d->id.'">
                                                 <b>Remaining Quantity : <span id="r_balance_qty_'.$d->product_id.'">'.$d->remaining_ready_to_delivery_qty.'</span></b></b>
                                                 <input type="text" name="overall_assigned_product_quantity[]" id="r_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                                 
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                                    
                                     $content.='<span id="r_expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                  

                                    $content.='</div>';
                                    
                                    
                                    // New One Starts Here 
                                    foreach($work_units as $works){
                                       
                                       
                                    if($works->type=="3" && $works->product_id==$d->product_id){
                                    
                                    
                                $content.='<div id="xyz_'.$works->id.'"> ';
                                
                                
                                
                                 $content.='<div class="col-md-4">
                                                <select id="products" name="products[]" class="form-control tip" style="width:100%;">
                                                    <option value="'.$works->product_id.'">'.$works->product_name.'</option>
                                                </select>
                                            </div>';     
                                    
                                      
                                    
                        $content.='<input type="hidden" name="products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;">
                                    <input type="hidden" name="ready_update_works_id[]" value="'.$works->id.'" class="form-control tip" style="width:100%;">';
                        
                        
         
                        
                        $content.='<div class="col-md-3">
                                    <input type="number" id="r_qty_'.$d->id.'" name="quantity[]" value="'.number_format("$works->quantity").'" class="form-control tip r_qqty_'.$d->product_id.' quantity" style="width:100%;" />
                                    </div>';
                        
                        $content.='<input type="hidden" id="total" name="total_quantity[]" value="'.number_format("$d->quantity").'" min="1" max="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" />';
                      
                        
                        
                        $content.='<div class="col-md-4">
                                        <select id="ready_not_ready" name="ready_not_ready[]" class="form-control tip" style="width:100%;">
                                            <option value="ready" '.(($works->ready_or_not_ready=="ready")?'selected="selected"':"").'>Ready</option>
                                            <option value="not_ready" '.(($works->ready_or_not_ready=="not_ready")?'selected="selected"':"").'>Not Ready</option>
                                       </select>
                                    </div>';
                                    
                                    
                                    
                        $content.='<div class="col-md-1" style="text-align: center; padding: 2px 0px 0px 5px;">';
                                    
                        $content.='<a onClick="deleteReadytAssoDeliveryAssignedID('.$works->id.','.$d->product_id.')" class="tip btn btn-danger btn-xs" style="padding: 5px 10px 5px 10px;"><i class="fa fa-trash-o"></i></a>';
                                    
                                    $content.='</div>';
                                    $content.='</div>';
                                    
                                    
                                    
                                    }
                                        
                                    }
                                    // New One Ends Here
                                    
                                    $content.='<div id="r_expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
                                   
                                    $content.='<div>';
                                    
                                    $content.='<script>
                                                $("#r_expand_sale_items_'.$d->product_id.'").on("click", function() {
                                                
                                                   html=`<div class="rowss"><div class="col-md-4"><select id="products" name="new_products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option></select></div><input type="hidden" name="new_products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"><div class="col-md-3"><input name="new_status[]" type="hidden" value="0"><input type="number" id="r_qty_'.$d->id.'" name="new_quantity[]" value="" min="1" max="'.number_format("$d->quantity").'" class="form-control tip r_qqty_'.$d->product_id.' quantity" style="width:100%;" /><input type="hidden" id="total" name="new_total_quantity[]" value="'.number_format("$d->quantity").'" min="1" max="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" /></div><div class="col-md-4"><select id="ready_not_ready" name="new_ready_not_ready[]" class="form-control tip" style="width:100%;"><option value="ready" '.(($d->ready_or_not_ready=="ready")?'selected="selected"':"").'>Ready</option><option value="not_ready" '.(($d->ready_or_not_ready=="not_ready")?'selected="selected"':"").'>Not Ready</option></select></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="vexpand_remove_'.$d->product_id.'"  class="btn btn-danger btn-xs r_expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;                              
                                                   $("#r_expand_conteent_'.$d->product_id.'").append(html);
                                                    
                                                });
                                              </script>';
                                    
                                    
                                    
                                    
                                    $content.='</div></div>';
                                    
                                    
                                    
                                   $content.='<script> 
                                                    
                                                
                                                $(function() {
                                                    $(document).on("click", ".r_expand_remove_'.$d->product_id.'", function() {
                                                        $(this).closest(".rowss").remove();
                                                        grand_total();
                                                    });
                                                    $(document).ready(function(){
                                                        grand_total();
                                                    });
                                                    $("body").on("keyup",".r_qqty_'.$d->product_id.'",function(){
                                                      var quantity=Number($(this).val());
                                                      console.log("ASSIGNED QUANTITY"+quantity);
                                                      grand_total();
                                                    });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".r_qqty_'.$d->product_id.'").each(function(){
                                                    tot+=Number($(this).val());
                                                  });
                                                  
                                                    var total = $("#r_total_product_quantity_'.$d->product_id.'").val();
                                                    console.log("Total Now Quantity:"+tot);
                                                    console.log("Total:"+tot);
                                                    grand_balance=total-tot;
                                                    console.log(grand_balance);
                                                    
                                                    $("#r_balance_qty_'.$d->product_id.'").text(grand_balance);
                                                    $("#r_assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                    
                                                   /* $("#s_qtty_'.$d->product_id.'").val(grand_balance);
                                                    $("#r_not_assign_qty_'.$d->id.'").val(grand_balance);*/
                                                    
                                                    if($("#r_balance_qty_'.$d->product_id.'").text() < 0){
                                                         $("#r_bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                         $("#ready_to_delivery_update").prop("disabled", true);
                                                    }else{
                                                        $("#r_bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                        $("#ready_to_delivery_update").prop("disabled", false);
                                                    }
                                                  
                                            }
                                        });
                                        
                                    </script>';
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                              
                                      
                                }
                                
                                    
                                    
                                    
                                }
                                
                                $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="ready_to_delivery_update" class="btn btn-info" value="Update"></div>';
                                                            
                        }  
                        /********* NEW METHOD READY TO DELIVERY ENDS HERE *****************/
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                 
                        
                    }else{
                        $content.='<center><h6>Ready to Delievry Status Fully Assign Working</h6></center>';
                    }
                }
            }        
                      
           else{
                $content.='No Data Available';
            }
            echo $content;
            
        }
    
    
    // (3333333333) ***********************  Latest NEWWWWWWWW Ready to Delievry Ends Here ******************************************* // 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // (44444444444) ***********************  Latest NEWWWWWWWWWWWWWWWWW Delivery Date Status Starts Here  ****************************************//
    public function get_all_delivery_items()
        {
            
            $sales_id = $this->input->get('sale_id');
            $date = $this->input->get('date');
            
            $mainsale_datas=$this->sales_model->getSaleMainTable($sales_id);
            $data=$this->sales_model->getSaleIemsBySaleID($sales_id);
            $work_units=$this->sales_model->getSaleWorkUnitsTable($sales_id);
            $sale_works_data=$this->sales_model->getSaleIemsBySaleIDWithWorksTable($sales_id);
            
            $user_data = $this->sales_model->getAllStaff();
            
            
            $content='<div class="row">
                                <div class="col-md-4">
                                    <label for="products">Products</label>
                                </div>
                                <div class="col-md-3">
                                    <label for="products">Qty</label>
                                </div>
                                <div class="col-md-4">
                                    <label for="products">Delivery Date</label>
                                </div>
                                <div class="col-md-1">
                                    <label for="products">Actions</label>
                                </div>
                      </div>';
                      
                      
                      
            if($mainsale_datas){
                foreach($mainsale_datas as $salemain){
                    
                    if($salemain->order_delivery_status==1){
                        
                        
                        /********* Cutting Status Starts ************/
                        $content.='<center><h6>NEWW Delievery Status Not Assign Working</h6></center>';
                        
                        
                        if($data){
                            $content.='<input type="hidden" value="'.$sales_id.'" id="product_delivery_date" name="sale_id" />
                                       <input type="hidden" value="8" id="measurement_status" name="measurement_status" />';
                        foreach($data as $d){               
                                if($d->product_type=="service" && $d->delivery_date_status=="1"){
                                    
                                       
                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                        
                        
                        
                     $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                   <b>Total Quantity: <span id="d_total_qty_'.$d->product_id.'"></span></b>
                                            <input type="text" name="overall_total_product_quantity[]"  id="d_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                               
                                                </div>';
                                                
                                    if($d->remaining_delivery_date_products==""){$d->remaining_delivery_date_products=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                     
                                                 <b id="d_bal_total_qty_'.$d->product_id.'">
                                            Remaining Quantity: <span id="d_balance_qty_'.$d->product_id.'">'.$d->remaining_delivery_date_products.'</span></b>
                                            <input type="text" name="overall_assigned_product_quantity[]" id="d_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                            
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                
                        $content.='<span id="d_expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                    

                                    $content.='</div>';
                                    
                            
                        
                      
                        
                        
                        $content.='<div id="d_expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
                        
                        $content.='</div>';
                        
                                  
                        $content.='<script>
                                        $(document).ready(function(){
                                            $("#d_total_qty_'.$d->product_id.'").text('.number_format("$d->quantity").'); 
                                        });
                                        
                                    $("#d_expand_sale_items_'.$d->product_id.'").on("click", function() {
                                    
                                        html=`<div class="rows"><div class="col-md-4"><select id="products" name="products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option></select></div><input type="hidden" name="products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"><div class="col-md-3"><input type="number" id="d_qty_'.$d->id.'" name="quantity[]" value="" min="1" max="'.number_format("$d->quantity").'" class="form-control tip d_qqty_'.$d->product_id.' quantity" style="width:100%;" /><input type="hidden" id="total" name="total_quantity[]" value="'.number_format("$d->quantity").'" min="1" max="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" /></div><div class="col-md-4"><input type="date" id="d_qty_'.$d->id.'" name="delivery_date[]" value=""  class="form-control tip" style="width:100%;" required /></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="d_expand_remove_'.$d->product_id.'"  class="btn btn-danger btn-xs d_expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                                
                                        $("#d_expand_conteent_'.$d->product_id.'").append(html);
                                    });
                                    
                                        $(function() {
                                            $(document).on("click", ".d_expand_remove_'.$d->product_id.'", function() {
                                            $(this).closest(".rows").remove();
                                            grand_total();
                                            });
                                            $("body").on("keyup",".d_qqty_'.$d->product_id.'",function(){
                                              var quantity=Number($(this).val());
                                              grand_total();
                                                  
                                            });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".d_qqty_'.$d->product_id.'").each(function(){
                                                    tot+=Number($(this).val());
                                                   });
                                                    var total = $("#d_total_product_quantity_'.$d->product_id.'").val();
                                                    console.log("Total:"+tot);
                                                    grand_balance=total-tot;
                                                    $("#d_balance_qty_'.$d->product_id.'").text(grand_balance);
                                                    if($("#d_balance_qty_'.$d->product_id.'").text() < 0){
                                                         $("#d_bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                         $("#delivery_partial_date_submit").prop("disabled", true);
                                                    }else{
                                                        $("#d_bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                        $("#delivery_partial_date_submit").prop("disabled", false);
                                                    }
                                                  
                                            }
                                        });
                                    </script>';
                                    
                }
                        }
                            $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="delivery_partial_date_submit" class="btn btn-success" value="Submit"></div>';
                                
                                    
                        }            
                        
                        
                        /********* Cutting Status Endss ************/
                        
                    }else if($salemain->order_delivery_status==2){
                        
                        
                        
                        /********* Cutting Status Partial Starts ************/
                        $content.='<center><h6>Delivery Partial Assign Working</h6></center>';
                        
                        
                        /********** NEWWWWWWWWWWWWW ************************/
                           if($data){
                            
                            $content.='<input type="hidden" value="'.$sales_id.'" id="product_delivery_date" name="sale_id" />
                                       <input type="hidden" value="88" id="measurement_status" name="measurement_status" />';
                                       
                                foreach($data as $d){
                                
                                
                                if($d->product_type=="service" && $d->delivery_date_status=="0" || $d->product_type=="service" && $d->delivery_date_status=="1"){
                                    
                                    
                                    
                                    $content.='<div class="row" style="padding: 6px 0px 6px 0px; border: 1px dashed gray; border-radius: 5px; margin-top: 15px;background: #a5eda5;">';
                                    
                                    
                                    $content.='<div class="col-md-4">
                                    
                                                    <b>Product Name : '.$d->product_name.'</b>
                                                    
                                                </div>';
                                                
                                   
                                    
                
                                   
                                    
                                    $content.='<div class="col-md-3">
                                                
                                                   <b>Total Quantity: <span id="d_total_qty_'.$d->product_id.'">'.number_format("$d->quantity").'</span></b>
                                            <input type="text" name="overall_total_product_quantity[]"  id="d_total_product_quantity_'.$d->product_id.'" value="'.number_format("$d->quantity").'" hidden>
                                               
                                                </div>';
                                                
                                    if($d->remaining_delivery_date_products==""){$d->remaining_delivery_date_products=number_format("$d->quantity");}
                                    
                                     $content.='<div class="col-md-3">
                                     
                                                 <b id="d_bal_total_qty_'.$d->product_id.'">
                                            Remaining Quantity: <span id="d_balance_qty_'.$d->product_id.'">'.$d->remaining_delivery_date_products.'</span></b>
                                            <input type="text" name="overall_assigned_product_quantity[]" id="d_assigned_product_quantity_'.$d->product_id.'" value="" hidden>
                                            
                                                </div>';
                                                
                                   
                                    
                                    $content.='<div class="col-md-2" style="text-align: center; padding: 0px 0px 5px 5px;">';
                                    
                                     $content.='<span id="d_expand_sale_items_'.$d->product_id.'"  class="btn btn-primary btn-xs expand_sale_items"><i class="fa fa-plus"></i> Add</span>';
                                  

                                    $content.='</div>';
                                    
                                    
                                    // New One Starts Here 
                                    foreach($work_units as $works){
                                        
                                        
                                    if($works->type=="4" && $works->product_id==$d->product_id){
                                    
                                    
                                    
                                    $content.='<div id="xyz_'.$works->id.'">'; 
                                    
                                    
                                     $content.='<div class="col-md-4">';
                        $content.='<select id="products" name="products[]" class="form-control tip" style="width:100%;">';
                        $content.='<option value="'.$works->product_id.'">'.$works->product_name.'</option>';
                        $content.='</select>';
                        $content.='</div>';
                        
                        
    
                        
                        $content.='<div class="col-md-3">';
                        $content.='<input type="number" id="d_qty_'.$works->id.'" name="quantity[]" value="'.number_format("$works->quantity").'" min="1" max="'.number_format("$works->quantity").'" class="form-control tip d_qqty_'.$d->product_id.' quantity" style="width:100%;" />
                                    <input type="hidden" id="d_tot_'.$works->id.'" name="total_quantity[]" value="'.number_format("$works->quantity").'" min="1" max="'.number_format("$works->quantity").'" class="form-control tip" style="width:100%;" />';
                        $content.='</div>';
                        
                        
         $content.='<input type="hidden" name="update_works_id[]" value="'.$works->id.'" class="form-control tip" style="width:100%;">';
                        
                        
                        $content.='<div class="col-md-4">
                                        <input type="date" id="d_qty_'.$works->id.'" name="delivery_date[]" value="'.$works->delivery_date.'"  class="form-control tip" style="width:100%;" required/>
                                   </div>';
                        
                        
                        $content.='<div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;">';
                         $content.='<a onClick="deleteDeliveryAssignedID('.$works->id.','.$d->product_id.')" class="tip btn btn-danger btn-xs" style="padding: 5px 10px 5px 10px;"><i class="fa fa-trash-o"></i></a>';
                                     $content.='<input type="hidden" id="work_sale_id" value="'.$works->sale_id.'" name="work_sale_id[]" class="form-control work_sale_id" style="width:100%;">
                                                <input type="hidden" id="work_product_id" value="'.$works->product_id.'" name="work_product_id[]" class="form-control work_product_id" style="width:100%;">';
                                    $content.='</div>';
                                    $content.='</div>';
                                    
                        
                                    
                                   
                                    
                                    
                                    }
                                        
                                    }
                                    // New One Ends Here
                                    
                                    $content.='<div id="d_expand_conteent_'.$d->product_id.'" style="padding: 0px 0px 0px 0px;"></div>';
                                    
                                   
                                    $content.='<div>';
                                    
                                    $content.='<script>
                                                 $("#d_expand_sale_items_'.$d->product_id.'").on("click", function() {
                                    
                                        html=`<div class="rows"><div class="col-md-4"><select id="products" name="new_products[]" class="form-control tip" style="width:100%;"><option value="'.$d->product_id.'">'.$d->product_name.'</option></select></div><input type="hidden" name="new_products_name[]" value="'.$d->product_name.'" class="form-control tip" style="width:100%;"><div class="col-md-3"><input type="number" id="d_qty_'.$d->id.'" name="new_quantity[]" value="" min="1" max="'.number_format("$d->quantity").'" class="form-control tip d_qqty_'.$d->product_id.' quantity" style="width:100%;" /><input name="new_status[]" type="hidden" value="0"><input type="hidden" id="total" name="new_total_quantity[]" value="'.number_format("$d->quantity").'" min="1" max="'.number_format("$d->quantity").'" class="form-control tip" style="width:100%;" /></div><div class="col-md-4"><input type="date" id="d_qty_'.$d->product_id.'" name="new_delivery_date[]" value=""  class="form-control tip" style="width:100%;" required/></div><div class="col-md-1" style="text-align: center; padding: 5px 0px 0px 5px;"><span id="d_expand_remove_'.$d->product_id.'"  class="btn btn-danger btn-xs d_expand_remove_'.$d->product_id.'"><i class="fa fa-close"></i></span></div></div>`;
                                                
                                        $("#d_expand_conteent_'.$d->product_id.'").append(html);
                                    });
                                    
                                              </script>';
                                    
                                    
                                    
                                    
                                    $content.='</div></div>';
                                    
                                    
                                    
                                   $content.='<script> 
                                                    
                                                
                                                $(function() {
                                                    $(document).on("click", ".d_expand_remove_'.$d->product_id.'", function() {
                                                        $(this).closest(".rows").remove();
                                                        grand_total();
                                                    });
                                                    $(document).ready(function(){
                                                        grand_total();
                                                    });
                                                    $("body").on("keyup",".d_qqty_'.$d->product_id.'",function(){
                                                      var quantity=Number($(this).val());
                                                      console.log("ASSIGNED QUANTITY"+quantity);
                                                      grand_total();
                                                    });
                                            function grand_total(){
                                                var tot=0;
                                                  $(".d_qqty_'.$d->product_id.'").each(function(){
                                                    tot+=Number($(this).val());
                                                  });
                                                  var total = $("#d_total_product_quantity_'.$d->product_id.'").val();
                                                    console.log("Total Now Quantity:"+tot);
                                                    grand_balance=total-tot;
                                                    
                                                    console.log(grand_balance);
                                                    $("#d_balance_qty_'.$d->product_id.'").text(grand_balance);
                                                    $("#d_assigned_product_quantity_'.$d->product_id.'").val(tot);
                                                    
                                                    $("#d_qtty_'.$d->product_id.'").val(grand_balance);
                                                    if($("#d_balance_qty_'.$d->product_id.'").text() < 0){
                                                         $("#d_bal_total_qty_'.$d->product_id.'").css({"color":"#FF0000"});
                                                         $("#d_delivery_update").prop("disabled", true);
                                                    }else{
                                                        $("#d_bal_total_qty_'.$d->product_id.'").css({"color":"black"});
                                                        $("#d_delivery_update").prop("disabled", false);
                                                    }
                                                  
                                            }
                                        });
                                        
                                    </script>';
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                              
                                      
                                }
                                
                                    
                                    
                                    
                                }
                                
                                   $content.='<div style="float: right; padding-top: 30px;"><input type="submit" id="d_delivery_update" class="btn btn-success" value="Update"></div>';
                                     
                        }   
                        /********** NEWWWWWWWWWWWWW ************************/
                        
                       
                       
                       
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                      
                        /********* Cutting Status Partial Endss ************/
                        
                    }else{
                        $content.='<center><h6>Delivery Status Fully Assign Working</h6></center>';
                    }
                }
            }        
                      
           
                
       
                        
                        
                        
                            
                            
                  /*$content.= $button;*/
           else{
                $content.='No Data Available';
            }
            echo $content;
            
        }
    // (4444444444) ***********************  Latest NEWWWWWWWWWWWWW Delivery Date Status Endss Here **************************************** // 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Histoty Starts Here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
    public function get_all_unit_status_history()
    {
            
            $sales_id = $this->input->get('sale_id');
            $mainsale_datas = $this->sales_model->getSaleMainTable($sales_id);
            $data=$this->sales_model->getSaleWorkUnitsTable($sales_id);
            $user_data = $this->sales_model->getAllStaff();
            
                      
            if($data){
                
                if(1==1){
                    
                    $content.='<div class="row" style="padding: 0px 0px 0px 0px; border: 1px double gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                    $content.='<style>table{border: 1px solid gray;width: 100%;}th,td{border: 1px solid gray;text-align: center;}th{padding: 5px;}</style>';
                    $content.='<table>
                                        <thead>
                                            <tr style="background: #c79e09; color: white;">
                                                <th colspan="4">Cutting Unit</th>
                                            </tr>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>User Name</th>
                                                <th>Quantity</th>
                                                <th>Assign Date</th>
                                            </tr>
                                        <thead>
                                        <tbody>';
                    foreach ($data as $d){
                    if($d->type==1 && $d->cutting_user_id!=0){
                        $user_content='<option value="0">Select User</option>';
                        if($user_data){
                            foreach($user_data as $user){
                                $user_content.='<option value="'.$user->id.'" '.(($d->cutting_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                            }
                        }else{
                            $user_content.='<option value="">No Data Available</option>';
                        }
                        
                        
                         
                        $content.='<tr>
                                                <td>
                                                    <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;color: red;text-align: center;font-weight: bold;font-size: 17px;" disabled="true">
                                                    <option value="'.$d->product_id.'">'.$d->product_name.'</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select id="user_name_'.$d->id.'" name="cutting_user_name[]" class="form-control tip" style="width:100%;" disabled="true">
                                                    '.$user_content.'
                                                </td>
                                                <td>'.number_format("$d->quantity").'</td>
                                                <td>'.date('d-m-Y', strtotime($d->cutting_assign_date)).'</td>
                                    </tr>';
                        
                    }
                }  
                    $content.='</tbody></table>';
                
                    $content.='</div>';
                }
                
                if(2==2){
                    
                    $content.='<div class="row" style="padding: 0px 0px 0px 0px; border: 1px double gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                    $content.='<style>table{border: 1px solid gray;width: 100%;}th,td{border: 1px solid gray;text-align: center;}th{padding: 5px;}</style>';
                    $content.='<table>
                                        <thead>
                                            <tr style="background: #c79e09; color: white;">
                                                <th colspan="4">Stitching Unit</th>
                                            </tr>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>User Name</th>
                                                <th>Quantity</th>
                                                <th>Assign Date</th>
                                            </tr>
                                        <thead>
                                        <tbody>';
                    foreach ($data as $d){
                    if($d->type==2 && $d->stitching_user_id!=0){
                        $user_content='<option value="0">Select User</option>';
                        if($user_data){
                            foreach($user_data as $user){
                                $user_content.='<option value="'.$user->id.'" '.(($d->stitching_user_id==$user->id)?'selected="selected"':"").'>'.$user->first_name. "" . $user->last_name.'</option>';
                            }
                        }else{
                            $user_content.='<option value="">No Data Available</option>';
                        }
                        
                        
                         
                        $content.='<tr>
                                                <td>
                                                    <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;color: red;text-align: center;font-weight: bold;font-size: 17px;" disabled="true">
                                                    <option value="'.$d->product_id.'">'.$d->product_name.'</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select id="user_name_'.$d->id.'" name="cutting_user_name[]" class="form-control tip" style="width:100%;" disabled="true">
                                                    '.$user_content.'
                                                </td>
                                                <td>'.number_format("$d->quantity").'</td>
                                                <td>'.date('d-m-Y', strtotime($d->stitching_assign_date)).'</td>
                                    </tr>';
                        
                    }
                }  
                    $content.='</tbody></table>';
                
                    $content.='</div>';
                }
                
                if(3==3){
                    
                    $content.='<div class="row" style="padding: 0px 0px 0px 0px; border: 1px double gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                    $content.='<style>table{border: 1px solid gray;width: 100%;}th,td{border: 1px solid gray;text-align: center;}th{padding: 5px;}</style>';
                    $content.='<table>
                                        <thead>
                                            <tr style="background: #c79e09; color: white;">
                                                <th colspan="3">Ready to Delivery Products</th>
                                            </tr>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>Quantity</th>
                                                <th>Delivery Status</th>
                                            </tr>
                                        <thead>
                                        <tbody>';
                    foreach ($data as $d){
                    if($d->type==3 && $d->ready_or_not_ready!=""){
                        
                        $content.='<tr>
                                                <td>
                                                    <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%;" disabled="true">
                                                    <option value="'.$d->product_id.'">'.$d->product_name.'</option>
                                                    </select>
                                                </td>
                                                <td>'.number_format("$d->quantity").'</td>
                                                <td>
                                                    <select id="ready_or_not_ready" name="ready_or_not_ready[]" class="form-control tip" style="width:100%;" disabled="true">
                                                        <option value="ready" '.(($d->ready_or_not_ready=='ready')?'selected="selected"':"").'>Ready</option>
                                                        <option value="not_ready" '.(($d->ready_or_not_ready=='not_ready')?'selected="selected"':"").'>Not Ready</option>
                                                    </select>
                                                </td>
                                    </tr>';
                        
                    }
                }  
                    $content.='</tbody></table>';
                
                    $content.='</div>';
                }
                
                if(4==4){
                    
                    $content.='<div class="row" style="padding: 0px 0px 0px 0px; border: 1px double gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                    $content.='<style>table{border: 1px solid gray;width: 100%;}th,td{border: 1px solid gray;text-align: center;}th{padding: 5px;}</style>';
                    $content.='<table>
                                        <thead>
                                            <tr style="background: #c79e09; color: white;">
                                                <th colspan="3">Order Delivery Date</th>
                                            </tr>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>Quantity</th>
                                                <th>Delivery Date</th>
                                            </tr>
                                        <thead>
                                        <tbody>';
                    foreach ($data as $d){
                    if($d->type==4){
                        
                        $content.='<tr>
                                                <td>
                                                    <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%; color: red; text-align: center;font-weight: bold;font-size: 17px;" disabled="true">
                                                    <option value="'.$d->product_id.'">'.$d->product_name.'</option>
                                                    </select>
                                                </td>
                                                <td>'.number_format("$d->quantity").'</td>
                                                <td>'.date('d-m-Y', strtotime($d->delivery_date)).'</td>
                                   </tr>';
                        
                    }
                }  
                    $content.='</tbody></table>';
                
                    $content.='</div>';
                }
                
                if(4==4){
                    
                    $content.='<div class="row" style="padding: 0px 0px 0px 0px; border: 1px double gray; border-radius: 5px; margin-top: 15px;background: #f1eded;">';
                    $content.='<style>table{border: 1px solid gray;width: 100%;}th,td{border: 1px solid gray;text-align: center;}th{padding: 5px;}</style>';
                    $content.='<table>
                                        <thead>
                                            <tr style="background: #c79e09; color: white;">
                                                <th colspan="3">Delivery Date</th>
                                            </tr>';
                                            foreach($mainsale_datas as $datas){
                                                
                                                if($datas->deliver_date=="" || $datas->deliver_date=="NULL"){
                                                    $delivery_date = "";
                                                }else{
                                                    $delivery_date = date('d-m-Y', strtotime($datas->deliver_date));
                                                }
                                                $content.='
                                                <tr style="background: cyan; font-size: 20px;">
                                                    <th style="width: 50%;">Delivery Date</th>
                                                    <th style="width: 50%;">'.$delivery_date.'</th>
                                                </tr';
                                            }
                                            
                             $content.='<thead>
                                        <tbody>';
                    foreach ($data as $d){
                    if($d->type==4){
                        
                        $content.='<tr>
                                                <td>
                                                    <select id="products" name="cutting_products[]" class="form-control tip" style="width:100%; color: red; text-align: center;font-weight: bold;font-size: 17px;" disabled="true">
                                                    <option value="'.$d->product_id.'">'.$d->product_name.'</option>
                                                    </select>
                                                </td>
                                                <td>'.number_format("$d->quantity").'</td>
                                                <td>'.date('d-m-Y', strtotime($d->delivery_date)).'</td>
                                   </tr>';
                        
                    }
                }  
                    $content.='</tbody></table>';
                
                    $content.='</div>';
                }
                
            }else{
                $content.='No Data Available';
            }
            echo $content;
            
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Histoty Endss Here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
    
    
    
    
    
    
    
    
    // *************************** Send SMS ******************************* //
    public function send_sms_form()
    {
            $sms_sales_id = $this->input->get('sms_sale_id');
            $sales_data = $this->sales_model->getSaleDetailsByID($sms_sales_id);
            
            if($sales_data){
                
                foreach($sales_data as $sales){
                    $content='<div class="row">
                                <div class="col-md-12">
                                    <label for="products">SMS Content</label>
                                    <textarea name="sms_content" rows="5" class="form-control">Hai, '.$sales->customer_name.'</textarea>
                                </div>
                                <div class="col-md-3" style="padding-top: 10px;">
                                    <input type="submit" name="send_sms" class="btn btn-primary" value="Send SMS"/>
                                </div>
                          </div>';
                }
                       
            }
            
                      
            
                      
        echo $content;    
    }
    
    
    // *************************** Send SMS ******************************* //
    
    
    
  

    

    
    
    
}


Zerion Mini Shell 1.0