diff --git a/app/Controllers/Jobcard.php b/app/Controllers/Jobcard.php index 006e2a4..41740a0 100644 --- a/app/Controllers/Jobcard.php +++ b/app/Controllers/Jobcard.php @@ -3,18 +3,25 @@ namespace App\Controllers; use App\Models\JobcardModel; -use App\Models\JobcardProductModel; +use App\Models\JobcardComplaintModel; +use App\Models\JobcardCustomComplaintModel; use App\Models\JobcardServiceModel; +use App\Models\JobcardProductModel; use App\Models\ProductModel; use App\Models\ServiceModel; use App\Models\ClientModel; +use App\Models\ComplaintModel; use App\Models\BikemodelsModel; use App\Models\BikemakeModel; use App\Models\VehicleModel; +use App\Models\SalesOrderModel; +use App\Models\SalesOrderProductModel; use Mpdf\Mpdf; class Jobcard extends BaseController { public $session; + public $BikemodelsModel; + public $BikemakeModel; public function __construct() { @@ -29,7 +36,6 @@ class Jobcard extends BaseController $JobcardModel = new JobcardModel(); $jobs=$JobcardModel->getJobCardsWithProductsAndService($this->session->get('logged_user_branch_id')); - $data['jobs']=$jobs; // echo "
";
         //    print_r($jobs);die;
@@ -51,7 +57,14 @@ class Jobcard extends BaseController
             $ProductModel = new ProductModel();
             $product=$ProductModel->where('isactive',1)->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
             $ServiceModel = new ServiceModel();
+            // echo $this->session->get('logged_user_branch_id');die;
             $service=$ServiceModel->where('isactive',1)->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
+            // echo json_encode($service);die;
+           
+            $ComplaintModel = new ComplaintModel();
+            $complaint=$ComplaintModel->where('isactive',1)->findAll();
+
+            $data['complaint']= $complaint;   
             $data['product_1']=$product;   
             $data['product']="";   
             $data['service_1']=$service;   
@@ -61,10 +74,26 @@ class Jobcard extends BaseController
 
         } else if ($job_card_id !== '0') 
         {
+            $productModel = new ProductModel();
+            $JobcardModel = new JobcardModel();
+            $JobcardProductModel = new JobcardProductModel();
+            $JobcardServiceModel = new JobcardServiceModel();
+            $JobcardComplaintModel = new JobcardComplaintModel();
+            $JobcardCustomComplaintModel = new JobcardCustomComplaintModel();
+
+            $servicedata = $JobcardModel->service_data($job_card_id, $this->session->get('logged_user_branch_id'));
+            foreach ($servicedata as $key => $value) {
+                $product_ids = json_decode($value['product_id'], true); 
+                $product = $JobcardProductModel->whereIn('job_card_product_id', $product_ids)->findAll();
+                echo json_encode($product);die;
+            }
+            
             $data['page_name']="Edit Job Card";
             $ClientModel = new ClientModel();
             $client=$ClientModel->where('isactive',1)->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
-            $JobcardModel = new JobcardModel(); 
+            $JobcardModel = new JobcardModel();
+            $productdata = $JobcardModel->job_card_edit($job_card_id, $this->session->get('logged_user_branch_id'));
+            
             $jobs=$JobcardModel->where('job_card_id', $job_card_id)->get()->getRowArray();
             $data['jobs']=$jobs;
             $VehicleModel = new VehicleModel();
@@ -84,7 +113,7 @@ class Jobcard extends BaseController
             $encodedModelId = json_encode([$modelId]);
 
             // Load the ProductModel
-            $productModel = new ProductModel();
+
 
             // Query the database to find the product based on make_id and model_id
             $product = $productModel->where('make_id', $makeId)
@@ -104,10 +133,8 @@ class Jobcard extends BaseController
                                     ->findAll();
             $data['product']=$product;
             $data['service']=$service;
-            $JobcardProductModel = new JobcardProductModel();
-            $jobs_product= $JobcardProductModel->where('job_card_id', $job_card_id)->findAll();
+            $jobs_product= $JobcardCustomComplaintModel->where('job_card_id', $job_card_id)->findAll();
             $data['jobs_product']=$jobs_product;
-            $JobcardServiceModel = new JobcardServiceModel();
             $jobs_service= $JobcardServiceModel->where('job_card_id', $job_card_id)->findAll();
             $data['jobs_service']=$jobs_service;
             $data['client']=$client; 
@@ -125,109 +152,160 @@ class Jobcard extends BaseController
         return view('job_card_form',$data);
     }
 
-   
+    
     public function add_jobs()
     {
+        // echo json_encode($this->request->getPost());die;
         $JobcardModel = new JobcardModel();
-        $ProductModel = new ProductModel();
-        $JobcardProductModel = new JobcardProductModel(); // Assuming you have a model for sales_order_product
-
+        $JobcardProductModel = new JobcardProductModel();
         $JobcardServiceModel = new JobcardServiceModel();
+        $JobcardComplaintModel = new JobcardComplaintModel();
+        $JobcardCustomComplaintModel = new JobcardCustomComplaintModel();
 
-        // Prepare data for sales order
-        $data = [
+        // Job card INSERT
+        $job_card_data = [
             'client_name' => $this->request->getPost('client_id'),
+            'mobile_no' => $this->request->getPost('mobile_no'),
             'vehicle_id'=>$this->request->getPost('vehicle_id'),
+            'branch_id'=> $this->session->get('logged_user_branch_id'),
             'billing_address' => $this->request->getPost('billing_address'),
             'billing_city' => $this->request->getPost('city'),
-            'mobile_no' => $this->request->getPost('mobile_no'),
             'billing_state' => $this->request->getPost('state'),
             'billing_postal_code' => $this->request->getPost('postalcode'),
-            // 'billing_country' => $this->request->getPost('country'),
-            // 'terms_condition' => $this->request->getPost('terms_condition'),
-            // 'subtotal' => $this->request->getPost('sub_total'),
-            // 'tax' => $this->request->getPost('invoice_tax'),
-        
-            'total'=> $this->request->getPost('grand_total'),
             'status'=> $this->request->getPost('status'),
-            'isactive' => 1, // Assuming this is a default value or handled separately
-            'branch_id'=> $this->session->get('logged_user_branch_id'),
+            'subtotal'=> $this->request->getPost('sub_total'),
+            'tax'=> $this->request->getPost('invoicetax'),
+            'total'=> $this->request->getPost('grand_total'),
+            'isactive' => 1
         ];
-        //  print_r($data);die;
         // Insert or update sales order
         $job_card_id = $this->request->getPost('job_card_id');
         if (!empty($job_card_id)) {
-            // print_r("hello");die;
-            $JobcardModel->update($job_card_id, $data);
+            $JobcardModel->update($job_card_id, $job_card_data);
         } else {
-            $JobcardModel->insert($data);
+            $JobcardModel->insert($job_card_data);
             $job_card_id = $JobcardModel->getInsertID(); // Get the last inserted ID
         }
         $orderNumber = 'JC-' . date('md') . '-' . str_pad($job_card_id, 5, '0', STR_PAD_LEFT);
         // print_r($orderNumber);die;
         // Update sales order with generated order number
         $JobcardModel->update($job_card_id, ['order_number' => $orderNumber]);
-        // Prepare data for sales order product
-        // echo "
";
-        // print_r($this->request->getPost());die;
-        $product_ids = $this->request->getPost('item_details');
-        $quantities = $this->request->getPost('quantity');
-        $discounts = $this->request->getPost('discount_amount');
-        $discount_types = $this->request->getPost('discount_type');
-        $amounts = $this->request->getPost('amount');
-        $itemtax=$this->request->getPost('item-tax');
-        $unitprice=$this->request->getPost('unit-price');
-        $job_card_product_id = $this->request->getPost('job_card_product_id');
-        //    print_r($product_ids);die;
 
-        // print_r($status);die;
-        $status = $this->request->getPost('status');    
-        foreach ($product_ids as $key => $product_id) {
-            $qty = isset($quantities[$key]) ? $quantities[$key] : 0;
-            if ($status == 'Created') {
-                // echo "hello";die;
-                $this->updateProductQuantity($ProductModel, $product_id, $qty);
-            }elseif ($status == 'Cancelled'){
-                // echo "cancel";die;
+        /********** INSERT SERVICE DATA ************/
+        $data = json_decode($_POST['products']);
+        $service_data = $data[0]->service;
+        $status = $this->request->getPost('status'); 
 
-                // echo"hello";die;
-                $this->addBackProductQuantity($ProductModel, $product_id, $qty);
-            }
+        foreach ($service_data as $item) {
+            // Insert into the service table
+            $service['job_card_id'] = $job_card_id;
+            $service['service_id'] = $item->service_id;
+            $service['qty'] = $item->quality;
+            $service['tax'] = $item->tax;
+            $service['amount'] = $item->amount;
+            $JobcardServiceModel->insert($service);
+            $service_id = $JobcardServiceModel->getInsertID();
         
-            $discount = isset($discounts[$key]) ? $discounts[$key] : 0;
-            $discount_type = isset($discount_types[$key]) ? $discount_types[$key] : '';
-            $tax=isset($itemtax[$key]) ? $itemtax[$key] : 0;
-            $rate=isset($unitprice[$key]) ? $unitprice[$key] : 0;
-            $amount = isset($amounts[$key]) ? $amounts[$key] : 0;
-            // $new_qty = $this->calculateUpdatedQuantity($ProductModel, $product_id, $qty);
-            // print_r($discount_type);die;
-            $product_data = [
-                'job_card_id' => $job_card_id,
-                'product_id' => $product_id,
-                'qty' => $qty,
-                'discount' => $discount,
-                'discount_type' => $discount_type,
-                'net_price'=>$rate,
-                'tax'=>$tax,
-                'amount' => $amount,
-                'isactive'=>1,
-            ];
-            //    print_r($product_data);die;
-            if (!empty($job_card_product_id[$key])) {
-                
-            
-                $JobcardProductModel->update($job_card_product_id[$key], $product_data);
-                
-            } else {
-                $JobcardProductModel->insert($product_data);
-            
+            // Insert into the product table
+            $id = [];
+            foreach ($item->product as $pt) {
+                $product['job_card_id'] =  $job_card_id;
+                $product['product_id'] = $pt->p_id;
+                $product['qty'] = $pt->qty;
+                $product['tax'] = $pt->tax;
+                $product['amount'] = $pt->amount;
+                $JobcardProductModel->insert($product);
+                $job_card_product_id = $JobcardProductModel->getInsertID();
+                array_push($id, $job_card_product_id);
+            }
+            $res = $JobcardServiceModel->update($service_id, ['product_id' => json_encode($id) ]);
+            if($res) {
+                $this->product_data_maintanence($id, $status);
+            }
+        }
+
+        /********** INSERT COMPLAINT DATA ************/
+        $complaint_data = $data[0]->complaint;
+
+        foreach ($complaint_data as $item) {
+            // Insert into the service table
+            $service['job_card_id'] = $job_card_id;
+            $service['complaint_id'] = $item->complaint_id;
+            $service['qty'] = $item->quality;
+            $service['tax'] = $item->tax;
+            $service['amount'] = $item->amount;
+            $JobcardComplaintModel->insert($service);
+            $complaint_id = $JobcardComplaintModel->getInsertID();
+        
+            // Insert into the product table
+            $id = [];
+            foreach ($item->product as $pt) {
+                $product['job_card_id'] =  $job_card_id;
+                $product['product_id'] = $pt->p_id;
+                $product['qty'] = $pt->qty;
+                $product['tax'] = $pt->tax;
+                $product['amount'] = $pt->amount;
+                $JobcardProductModel->insert($product);
+                $job_card_product_id = $JobcardProductModel->getInsertID();
+                array_push($id, $job_card_product_id);
+            }
+            $res =$JobcardComplaintModel->update($complaint_id, ['product_id' => json_encode($id) ]);
+            if($res) {
+                $this->product_data_maintanence($id, $status);
+            }
+        }
+
+        /********** INSERT CUSTOM COMPLAINT DATA ************/
+        $c_complaint_data = $data[0]->custom_complaint;
+
+        foreach ($c_complaint_data as $item) {
+            // Insert into the service table
+            $service['job_card_id'] = $job_card_id;
+            $service['complaint_name'] = $item->complaint_id;
+            $service['qty'] = $item->quality;
+            $service['tax'] = $item->tax;
+            $service['amount'] = $item->amount;
+            $JobcardCustomComplaintModel->insert($service);
+            $cc_complaint_id = $JobcardCustomComplaintModel->getInsertID();
+        
+            // Insert into the product table
+            $id = [];
+            foreach ($item->product as $pt) {
+                $product['job_card_id'] =  $job_card_id;
+                $product['product_id'] = $pt->p_id;
+                $product['qty'] = $pt->qty;
+                $product['tax'] = $pt->tax;
+                $product['amount'] = $pt->amount;
+                $JobcardProductModel->insert($product);
+                $job_card_product_id = $JobcardProductModel->getInsertID();
+                array_push($id, $job_card_product_id);
+            }
+            $res =$JobcardCustomComplaintModel->update($cc_complaint_id, ['product_id' => json_encode($id) ]);
+            if($res) {
+                $this->product_data_maintanence($id, $status);
             }
-                    
         }
 
         return redirect()->to('job_card_index');
     }
 
+    public function product_data_maintanence($id_array,$status)
+    {
+        $ProductModel = new ProductModel();
+        $JobcardProductModel = new JobcardProductModel();
+
+        foreach ($id_array as $key => $product_id) {
+            $product = $JobcardProductModel->find($product_id);
+            $qty = isset($product['qty']) ? $product['qty'] : 0;
+            if ($status == 'Created') {
+                $this->updateProductQuantity($ProductModel, $product['product_id'], $qty);
+            }elseif ($status == 'Cancelled'){
+                $this->addBackProductQuantity($ProductModel, $product['product_id'], $qty);
+            }
+                    
+        }
+    }
+
     private function addBackProductQuantity($ProductModel, $product_id, $sold_qty)
     {
         // Fetch current product quantity
diff --git a/app/Models/JobOtherWorksModel.php b/app/Models/JobOtherWorksModel.php
deleted file mode 100644
index 54801ed..0000000
--- a/app/Models/JobOtherWorksModel.php
+++ /dev/null
@@ -1,11 +0,0 @@
-select('job_card.*, COUNT(sales_order_product.job_card_id) AS product_count,vehicle.reg_no');
-        // $this->select('job_card.*, COUNT(job_card_product.job_card_id) AS product_count, COUNT(job_card_service.job_card_id) AS service_count,vehicle.reg_no');
+        // $this->select('job_card.*, COUNT(job_card_service.job_card_id) AS product_count, COUNT(job_card_service.job_card_id) AS service_count,vehicle.reg_no');
         $this->select('job_card.*, COUNT(job_card_product.job_card_id) AS product_count, vehicle.reg_no');
         
         // Join the sales_order_product table based on job_card_id
+        $this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id');
         $this->join('job_card_product', 'job_card_product.job_card_id = job_card.job_card_id');
-        // $this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id');
         $this->join('vehicle', 'vehicle.vehicle_id = job_card.vehicle_id');
         
         // Group by job_card_id to get count of products per sales order
         $this->groupBy('job_card.job_card_id');
     
         // Add condition to fetch only active sales orders
-        $this->where('job_card_product.isactive', 1);
+        $this->where('job_card_service.isactive', 1);
         // $this->where('job_card_service.isactive', 1);
         $this ->where('job_card.branch_id',$logged_user_branch_id);
         $this->orderBy('job_card.job_card_id','DESC');
         // Get the results
         return $this->findAll();
     }
+
+    public function service_data($job_card_id, $logged_user_branch_id)
+    {
+        $this->select('job_card.*, job_card_service.*');
+        $this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id');
+        $this->where('job_card_service.isactive', 1);
+        $this ->where('job_card.job_card_id',$job_card_id);
+        $this ->where('job_card.branch_id',$logged_user_branch_id);
+        return $this->findAll();
+    }
     
 }
\ No newline at end of file
diff --git a/app/Models/JobcardProductModel.php b/app/Models/JobcardProductModel.php
index 1d28de1..64b08ce 100644
--- a/app/Models/JobcardProductModel.php
+++ b/app/Models/JobcardProductModel.php
@@ -5,7 +5,5 @@ class JobcardProductModel extends Model
 {
     protected $table = 'job_card_product';
     protected $primaryKey = 'job_card_product_id';
-    protected $allowedFields = ['job_card_product_id','item_name','qty','total','net_price','selling_price','discount','job_card_id','isactive'];
-
-    
+    protected $allowedFields = ['job_card_product_id','job_card_id' , 'product_id', 'qty', 'tax', 'amount', 'isactive'];
 }
\ No newline at end of file
diff --git a/app/Models/JobcardServiceModel.php b/app/Models/JobcardServiceModel.php
index 71b44c5..c86ef92 100644
--- a/app/Models/JobcardServiceModel.php
+++ b/app/Models/JobcardServiceModel.php
@@ -5,7 +5,7 @@ class JobcardServiceModel extends Model
 {
     protected $table = 'job_card_service';
     protected $primaryKey = 'job_card_service_id';
-    protected $allowedFields = ['job_card_service_id','service_name','qty','total','selling_price','discount','net_price','job_card_id','isactive'];
+    protected $allowedFields = ['job_card_service_id', 'job_card_id', 'service_id', 'product_id', 'qty','tax','amount', 'isactive'];
 
     
 }
\ No newline at end of file
diff --git a/app/Views/job_card_form.php b/app/Views/job_card_form.php
index f3a7355..dbd49bc 100644
--- a/app/Views/job_card_form.php
+++ b/app/Views/job_card_form.php
@@ -19,11 +19,12 @@
     
-
" method="post"> + " method="post"> +
-
+
' ?> @@ -40,36 +41,19 @@
-
+
-
+
-
+
-
- - -

Billing Address Details :


@@ -91,6 +75,25 @@
+
+
+ + +
+

Item Details

@@ -98,6 +101,7 @@ + @@ -115,8 +119,11 @@ +
Item Details Qty Rate
- @@ -161,12 +168,15 @@
@@ -200,7 +210,7 @@
- +
@@ -387,8 +397,8 @@ }); @@ -409,11 +419,192 @@ return validateProductAdded(); }); + $(document).on('keyup change', '.item-amount', function() { + // Calculate amount and update amount input field + updateCalculations(); + }); + var products = ; var services = ; + var complaint = ; + var mapTable; + function data_contruction_for_save() { + + var dataMapArray = [{ 'service': [], 'complaint': [], 'custom_complaint': [] }]; + + // Select all elements within the tbody of the table with id productItemTable + var tableRows = document.querySelectorAll("#productItemTable tbody tr"); + + // Loop through each element + tableRows.forEach(function(row) { + var secondTd = row.querySelector("td:nth-child(2)"); + var thirdTd = row.querySelector("td:nth-child(3)"); + var fifthTd = row.querySelector("td:nth-child(5)"); + var sixthTd = row.querySelector("td:nth-child(6)"); + + // Check if the second element contains a element + var className = selectElement.className; + + // Get the selected value of the ' + + '' + + '' + + ' ' + + '' + + '' + + '' + + ''; + + + // $('#productItemTable tbody').append(newRow); + $(newRow).insertAfter(tr); + initializeSelect2(); - // Find product details from JSON - console.log(products); - var product = products.find(function(item) { - return item.product_id == productId; }); - - // Access unit price from product and set it as rate - var unitPrice = parseFloat(product.unit_price); // Convert to float if necessary - $(this).closest('tr').find('.item-rate').val(unitPrice); - - // Calculate amount and update amount input field - calculateAmount($(this).closest('tr')); - }); - - - + } // Event listener for quantity change - $(document).on('input', '.item-quantity', function() { + $(document).on('input change', '.item-quantity', async function() { + // let promise = new Promise((resolve,reject) => { calculateAmount($(this).closest('tr')); - updateCalculations(); // Added for updating calculations when quantity changes + // resolve() + // }) + // promise.then(() => { + updateCalculations(); // Added for updating calculations when quantity changes + // }); }); - - // Function to calculate subtotal function calculateSubtotal() { @@ -554,9 +772,9 @@ updateCalculations(); // Event listener for "Add More Item" button - $('#addProduct').click(function() { - // console.log(productarray); - var newRow = '' + ''; + var newRow = ' ' + - '' + + '' + + '' + + '' + + '' + + '' + + '
' + + ''; + + + $('#productItemTable tbody').append(newRow); + initializeSelect2(); + + + }); + + $('#addcomplaint').click(function() { + complaint = ; + // console.log(productarray); + var newRow = ' ' + + '' + '' + '' + '' + '' + - '
' + + '
'+ + '
'+ + '
'+ + '
'+ + ''+ + ''+ + ''+ + ''+ + '
'+ + '' + + ''; + + + $('#productItemTable tbody').append(newRow); + initializeSelect2(); + + + }); + + $('#addCustomComplaint').click(function() { + complaint = ; + // console.log(productarray); + var newRow = ' ' + + '' + + '' + + '' + + '' + + '' + + '
'+ + '
'+ + '
'+ + '
'+ + ''+ + ''+ + ''+ + ''+ + '
'+ + '' + ''; @@ -615,6 +899,20 @@ + + - -->