555 lines
21 KiB
PHP
Executable File
555 lines
21 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\PurchaseOrderModel;
|
|
use App\Models\ProductModel;
|
|
use App\Models\VendorModel;
|
|
use App\Models\BranchModel;
|
|
use App\Models\BusinessModel;
|
|
use App\Models\PurchaseOrderChildModel;
|
|
use Mpdf\Mpdf;
|
|
use App\Helpers\MailHelper;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
|
class Purchase extends BaseController
|
|
{
|
|
public $session;
|
|
protected $PurchaseOrderModel;
|
|
protected $ProductModel;
|
|
protected $VendorModel;
|
|
protected $BranchModel;
|
|
protected $BusinessModel;
|
|
protected $PurchaseOrderChildModel;
|
|
use ResponseTrait;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->session = session();
|
|
$this->PurchaseOrderModel = new PurchaseOrderModel();
|
|
$this->ProductModel = new ProductModel();
|
|
$this->VendorModel = new VendorModel();
|
|
$this->BranchModel = new BranchModel();
|
|
$this->BusinessModel = new BusinessModel();
|
|
$this->PurchaseOrderChildModel = new PurchaseOrderChildModel();
|
|
}
|
|
|
|
public function purchase_index()
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
|
|
|
|
$data['purchase'] = $this->PurchaseOrderModel->getPurchaseOrdersWithProducts($this->session->get('logged_user_branch_id'));
|
|
|
|
|
|
return view('purchase_list',$data);
|
|
}
|
|
|
|
public function new_purchase($purchase_order_id)
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
|
|
if ($purchase_order_id === '0') {
|
|
|
|
$data['purchase'] = [];
|
|
|
|
|
|
|
|
$vendor = $this->VendorModel->where('branch_id',$this->session->get('logged_user_branch_id'))
|
|
->where('isactive', 1)
|
|
->select('vendor_id,vendor_name')
|
|
->findAll();
|
|
|
|
|
|
|
|
|
|
|
|
$branch = $this->BranchModel->where('branch_id', $this->session->get('logged_user_branch_id'))->get()->getRowArray();
|
|
$business = $this->BusinessModel->where('business_id', $this->session->get('logged_user_business_id'))->get()->getRowArray();
|
|
$product = $this->ProductModel->where('isactive',1)
|
|
->where('branch_id',$this->session->get('logged_user_branch_id'))
|
|
->findAll();
|
|
$data['product']=$product;
|
|
$data['branch'] = $branch;
|
|
$data['business'] = $business;
|
|
|
|
$data['page_name']="Add Purchase Order";
|
|
$data['vendor']=$vendor;
|
|
|
|
$data['vendor_id'] = [];
|
|
} else if ($purchase_order_id !== '0') {
|
|
$data['page_name']="Edit Purchase Order";
|
|
|
|
|
|
$purchase = $this->PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
|
|
|
|
|
|
$vendor = $this->VendorModel->where('branch_id',$this->session->get('logged_user_business_id'))
|
|
->where('isactive', 1)
|
|
->select('vendor_id,vendor_name')
|
|
->findAll();
|
|
$data['vendor']=$vendor;
|
|
|
|
$data['purchase'] = $purchase;
|
|
|
|
|
|
|
|
|
|
$vendor = $this->VendorModel->findAll();
|
|
$data['vendor'] = $vendor;
|
|
|
|
$data['vendor_id'] = $purchase['vendor_id'];
|
|
// print_r($vendor);die;
|
|
$vendorId=$purchase['vendor_id'];
|
|
$encodedVendorId = json_encode([$vendorId]);
|
|
|
|
// Query the database to find the product based on make_id and model_id
|
|
// $product = $this->ProductModel->select('products.*, manufacturer.manufacturer_name')
|
|
// ->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id')
|
|
// ->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
|
|
// ->findAll();
|
|
|
|
$data['product']=$this->gatherVendorProducts($encodedVendorId);
|
|
// echo "<pre>";
|
|
// print_r($data['product']);die;
|
|
|
|
|
|
|
|
|
|
$purchase_product = $this->PurchaseOrderChildModel->where('purchase_order_id', $purchase_order_id)->findAll();
|
|
$data['purchase_product'] = $purchase_product;
|
|
|
|
}
|
|
$productAll = $this->ProductModel->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id','left')
|
|
->where('products.isactive',1)
|
|
->findAll();
|
|
$data['productAll']= json_encode($productAll);
|
|
$data['purchase_order_id']= $purchase_order_id;
|
|
echo view('purchase_form',$data);
|
|
}
|
|
|
|
|
|
public function reorder_purchase($purchase_order_id)
|
|
{
|
|
|
|
// $data['page_name']="Edit Purchase Order";
|
|
|
|
|
|
// $purchase = $this->PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
|
|
|
|
|
|
// $vendor = $this->VendorModel->where('branch_id',$this->session->get('logged_user_business_id'))->select('vendor_id,vendor_name')->findAll();
|
|
// $data['vendor']=$vendor;
|
|
|
|
// $data['purchase'] = $purchase;
|
|
|
|
|
|
// $vendor = $this->VendorModel->findAll();
|
|
// $data['vendor'] = $vendor;
|
|
|
|
// $data['vendor_id'] = $purchase['vendor_id'];
|
|
// // print_r($vendor);die;
|
|
// $vendorId=$purchase['vendor_id'];
|
|
// $encodedVendorId = json_encode([$vendorId]);
|
|
|
|
|
|
// // Query the database to find the product based on make_id and model_id
|
|
// $product = $this->productModel->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
|
|
// ->findAll();
|
|
// $data['product']=$product;
|
|
// // print_r($product);die;
|
|
|
|
$data['purchase'] = [];
|
|
|
|
$purchase = $this->PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
|
|
|
|
|
|
$vendor = $this->VendorModel->where('branch_id',$this->session->get('logged_user_business_id'))->select('vendor_id,vendor_name')->findAll();
|
|
$data['vendor']=$vendor;
|
|
|
|
unset($purchase['purchase_order_id']);
|
|
$data['purchase'] = $purchase;
|
|
|
|
|
|
|
|
|
|
$vendor = $this->VendorModel->findAll();
|
|
$data['vendor'] = $vendor;
|
|
|
|
// $data['vendor_id'] = $purchase['vendor_id'];
|
|
|
|
|
|
|
|
|
|
$branch = $this->BranchModel->where('branch_id', $this->session->get('logged_user_branch_id'))->get()->getRowArray();
|
|
$business = $this->BusinessModel->where('business_id', $this->session->get('logged_user_business_id'))->get()->getRowArray();
|
|
|
|
$product = $this->ProductModel->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id','left')
|
|
->where('products.isactive',1)
|
|
->where('products.branch_id',$this->session->get('logged_user_branch_id'))->findAll();
|
|
$data['product']=$product;
|
|
$data['branch'] = $branch;
|
|
$data['business'] = $business;
|
|
$data['reorder'] = true;
|
|
$data['page_name']="Add Purchase Order";
|
|
$data['vendor']=$vendor;
|
|
$data['vendor_id'] = [];
|
|
|
|
|
|
|
|
$purchase_product = $this->PurchaseOrderChildModel
|
|
->select('product_id, qty, received_qty, isactive, is_selected')
|
|
->where('purchase_order_id', $purchase_order_id)
|
|
->where('(is_selected = 0 OR (qty > received_qty AND received_qty != 0) )')
|
|
->findAll();
|
|
|
|
$data['purchase_product'] = $purchase_product;
|
|
|
|
$productAll = $this->ProductModel->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id','left')
|
|
->where('products.isactive',1)
|
|
->findAll();
|
|
$data['productAll']= json_encode($productAll);
|
|
|
|
|
|
|
|
$data['purchase_order_id']= $purchase_order_id;
|
|
echo view('purchase_form',$data);
|
|
}
|
|
|
|
public function add_purchase()
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
|
|
|
|
$data = [
|
|
'vendor_id' => $this->request->getPost('vendor_id'),
|
|
'order_date' => $this->request->getPost('order_date'),
|
|
'contact_person_name' => $this->request->getPost('contact_person_name'),
|
|
'contact_person_mobile'=> $this->request->getPost('contact_person_mobile'),
|
|
'billing_address' => $this->request->getPost('billing_address'),
|
|
'billing_state' => $this->request->getPost('billing_state'),
|
|
'billing_city' => $this->request->getPost('billing_city'),
|
|
'billing_postal_code' => $this->request->getPost('billing_postal_code'),
|
|
'shipping_address' => $this->request->getPost('shipping_address'),
|
|
'shipping_state' => $this->request->getPost('shipping_state'),
|
|
'shipping_city' =>$this->request->getPost('shipping_city'),
|
|
'shipping_postal_code' => $this->request->getPost('shipping_postal_code'),
|
|
'bill_number' => $this->request->getPost('bill_number')? $this->request->getPost('bill_number') : '',
|
|
'return_date' => $this->request->getPost('return_date') ? $this->request->getPost('return_date') : '',
|
|
'branch_id'=> $this->session->get('logged_user_branch_id'),
|
|
];
|
|
|
|
if($this->request->getPost('purchase_status') != ''){
|
|
$data['status'] = $this->request->getPost('purchase_status');
|
|
}
|
|
|
|
|
|
|
|
$purchase_order_id = $this->request->getPost('purchase_order_id');
|
|
// echo $purchase_order_id;die;
|
|
if (!empty($purchase_order_id)) {
|
|
|
|
$this->PurchaseOrderModel->update($purchase_order_id, $data);
|
|
} else {
|
|
|
|
$this->PurchaseOrderModel->insert($data);
|
|
$purchase_order_id = $this->PurchaseOrderModel->getInsertID();
|
|
}
|
|
|
|
$orderNumber = 'PO-' . date('md') . '-' . str_pad($purchase_order_id, 5, '0', STR_PAD_LEFT);
|
|
|
|
// Update sales order with generated order number
|
|
$this->PurchaseOrderModel->update($purchase_order_id, ['order_number' => $orderNumber]);
|
|
|
|
// Prepare data for sales order product
|
|
$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');
|
|
$selected_items=$this->request->getPost('selected_items');
|
|
$purchase_order_child_id = $this->request->getPost('purchase_order_child_id');
|
|
|
|
$status = $this->request->getPost('purchase_status');
|
|
foreach ($product_ids as $key => $product_id) {
|
|
$qty = isset($quantities[$key]) ? $quantities[$key] : 0;
|
|
$selected_item = (isset($selected_items[$key]) && $selected_items[$key] == 'checked') ? 1 : 0;
|
|
// print_r($selected_item);die;
|
|
if ($status == 'Received' && $selected_item == 1) {
|
|
$this->addBackProductQuantity( $product_id, $qty);
|
|
}
|
|
|
|
$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;
|
|
$child_data = [
|
|
'purchase_order_id' => $purchase_order_id,
|
|
'product_id' => $product_id,
|
|
// 'qty' => $qty,
|
|
// 'received_qty' => $qty,
|
|
// 'discount' => $discount,
|
|
// 'discount_type' => $discount_type,
|
|
'net_price'=>$rate,
|
|
'tax'=>$tax,
|
|
'amount' => $amount,
|
|
'is_selected'=>$selected_item,
|
|
'isactive'=>1,
|
|
];
|
|
|
|
if($status == 'Received') {
|
|
$child_data['received_qty'] = $qty;
|
|
}
|
|
else{
|
|
$child_data['qty'] = $qty;
|
|
}
|
|
|
|
|
|
|
|
if (!empty($purchase_order_child_id[$key])) {
|
|
|
|
|
|
$this->PurchaseOrderChildModel->update($purchase_order_child_id[$key], $child_data);
|
|
|
|
} else {
|
|
|
|
$this->PurchaseOrderChildModel->insert($child_data);
|
|
|
|
// send purhase data through mail
|
|
|
|
$vendor = $this->VendorModel->where('vendor_id',$this->request->getPost('vendor_id'))->findAll();
|
|
// $mail_status = $this->download_purchase_invoice($purchase_order_id, $vendor[0]['vendor_email']);
|
|
|
|
// echo $mail_status;die;
|
|
// $_SESSION['mail_status'] = $mail_status;
|
|
}
|
|
|
|
}
|
|
|
|
return redirect()->to('purchase_index');
|
|
}
|
|
|
|
|
|
private function addBackProductQuantity($product_id, $sold_qty)
|
|
{
|
|
// Fetch current product quantity
|
|
$product = $this->ProductModel->find($product_id);
|
|
|
|
$data['qty_stock'] = $product['qty_stock'] + $sold_qty;
|
|
|
|
if($product['per'] == 'barrel')
|
|
{
|
|
$data['barrel'] = $data['qty_stock'] / $product['ltr_per_barrel'];
|
|
}
|
|
else if($product['per'] == 'box')
|
|
{
|
|
$data['box'] = $data['qty_stock'] / $product['qty_per_box'];
|
|
}
|
|
|
|
// Update quantity in Product table
|
|
$this->ProductModel->update($product_id, $data );
|
|
}
|
|
|
|
public function delete_purchase($purchase_order_id)
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
$where = ['purchase_order_id' => $purchase_order_id, 'isactive' => 1 ]; // Assuming 'isactive' is a column in your database
|
|
$existingPurchase = $this->PurchaseOrderModel->where($where)->first();
|
|
|
|
if ($existingPurchase) {
|
|
$data['isactive'] = 0;
|
|
|
|
if ($this->PurchaseOrderModel->update($purchase_order_id, $data)) {
|
|
session()->setFlashdata('success', 'Deleted successfully.');
|
|
$this->logger->info("Purchase Order: has been Inactivated successfully. Inactivated ID = ".$purchase_order_id);
|
|
}
|
|
}
|
|
|
|
|
|
return redirect()->to('purchase_index');
|
|
}
|
|
|
|
|
|
|
|
|
|
public function get_vendor($vendor_id)
|
|
{
|
|
|
|
|
|
|
|
$products =$this->VendorModel->where('vendor_id',$vendor_id)->select('vendor_name,contact_person_name1,contact_person_mobile1')->get()->getRowArray();
|
|
|
|
|
|
return json_encode($products);
|
|
|
|
}
|
|
|
|
public function getVendorProducts()
|
|
{
|
|
// Retrieve make_id and model_id from the request
|
|
// $makeId = $this->request->getPost('');
|
|
$vendorId = $this->request->getPost('vendor_id');
|
|
|
|
|
|
$VendorData = $this->VendorModel->where('vendor_id',$vendorId)->first();
|
|
|
|
|
|
// Encode the modelId before searching
|
|
$encodedVendorId = json_encode([$vendorId]);
|
|
|
|
|
|
|
|
// Query the database to find the product based on make_id and model_id
|
|
$product = $this->gatherVendorProducts($encodedVendorId);
|
|
$CrossVerify = "";
|
|
// $CrossVerify = $this->ProductModel->getLastQuery()->getQuery();
|
|
// print_r($product);die;
|
|
return $this->response->setJSON([
|
|
'product' => $product ?: [],
|
|
'VendorData' => $VendorData ?: [],
|
|
'CrossVerify' => $CrossVerify ?: ''
|
|
]);
|
|
}
|
|
|
|
public function gatherVendorProducts($encodedVendorId)
|
|
{
|
|
$product = $this->ProductModel
|
|
->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id', 'left')
|
|
->where('products.isactive', 1)
|
|
->where('JSON_VALID(products.vendor) > 0', null, false)
|
|
->where("JSON_CONTAINS(products.vendor, '{$encodedVendorId}')", null, false)
|
|
->findAll();
|
|
return $product;
|
|
}
|
|
|
|
public function delete_purchase_product()
|
|
{
|
|
$purchase_order_child_id = $this->request->getPost('purchase_order_child_id');
|
|
|
|
if (!empty($purchase_order_child_id)) {
|
|
|
|
$data['isactive'] = 0;
|
|
|
|
if ($this->PurchaseOrderChildModel->update($purchase_order_child_id, $data)) {
|
|
// Return success response
|
|
return $this->response->setJSON(['success' => true]);
|
|
}
|
|
}
|
|
|
|
// Return error response if deletion fails
|
|
return $this->response->setJSON(['success' => false]);
|
|
}
|
|
public function download_purchase_invoice($purchase_order_id, $mail = null)
|
|
{
|
|
// Fetch the invoice data based on $invoice_id
|
|
$data = $this->PurchaseOrderModel->getPurchasePdf($purchase_order_id);
|
|
|
|
$items = $this->PurchaseOrderModel->getPurchaseOrderProductsForPdf($purchase_order_id);
|
|
// Create an mPDF object
|
|
$mpdf = new Mpdf([
|
|
'mode' => '',
|
|
'format' => 'A5',
|
|
'default_font_size' => 0,
|
|
'default_font' => '',
|
|
|
|
// 'margin_right' => 1,
|
|
'margin_top' => 6,
|
|
'margin_bottom' => 6,
|
|
'margin_header' => 6,
|
|
'margin_footer' =>-30,
|
|
'orientation' => 'P',
|
|
]);
|
|
$mpdf->autoLangToFont = true; $mpdf->autoScriptToLang = true;
|
|
// Set PDF properties
|
|
$mpdf->SetTitle('Invoice');
|
|
$mpdf->SetAuthor($data[0]->branch_name);
|
|
$mpdf->SetCreator('');
|
|
// Generate the PDF content (HTML) with data
|
|
$html = view('invoice_purchase_template', ['sales' => $data,'items'=>$items]);
|
|
// Load HTML into the mPDF instance
|
|
$mpdf->WriteHTML($html);
|
|
if($mail == null)
|
|
{
|
|
// Output the PDF to the browser for download
|
|
$mpdf->Output($data[0]->order_number.'-' . date('Y-m-d H-i-s') . '.pdf', 'D');
|
|
}
|
|
else
|
|
{
|
|
$pdfFilePath = $data[0]->order_number.'-' . date('Y-m-d-H-i-s') . '.pdf';
|
|
$mpdf->Output($pdfFilePath, 'F');
|
|
|
|
$data['mail'] = $mail;
|
|
$data['subject'] = "The Mechanic";
|
|
$data['message'] = "test";
|
|
$data['attachment'] = $pdfFilePath;
|
|
$mail_response = MailHelper::send_email($data);
|
|
if(json_decode($mail_response)->code == 200)
|
|
{
|
|
unlink($pdfFilePath);
|
|
}
|
|
return json_decode($mail_response)->code;
|
|
}
|
|
|
|
}
|
|
public function preview_purchase($purchase_order_id)
|
|
{
|
|
|
|
$data['purchase'] = $this->PurchaseOrderModel->getPurchasePdf($purchase_order_id);
|
|
$data['items'] = $this->PurchaseOrderModel->getPurchaseOrderProductsForPdf($purchase_order_id);
|
|
|
|
$html = view('purchase_pdf_template', $data);
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $html],200);
|
|
}
|
|
|
|
public function download_purchase($purchase_order_id)
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
|
|
$data['purchase'] = $this->PurchaseOrderModel->getPurchasePdf($purchase_order_id);
|
|
$data['items'] = $this->PurchaseOrderModel->getPurchaseOrderProductsForPdf($purchase_order_id);
|
|
|
|
$mpdf = new Mpdf([
|
|
'mode' => '',
|
|
'format' => 'A4',
|
|
'default_font_size' => 0,
|
|
'default_font' => '',
|
|
// 'margin_right' => 1,
|
|
'margin_top' => 6,
|
|
'margin_bottom' => 6,
|
|
'margin_header' => 6,
|
|
'margin_footer' =>-30,
|
|
'orientation' => 'P',
|
|
]);
|
|
|
|
$mpdf->autoLangToFont = true; $mpdf->autoScriptToLang = true;
|
|
// Set PDF properties
|
|
$mpdf->SetTitle('Purchase Order');
|
|
$mpdf->SetAuthor($data['purchase'][0]->order_number);
|
|
$mpdf->SetCreator('');
|
|
|
|
|
|
// Generate the PDF content (HTML) with data
|
|
$html = view('purchase_pdf_template', $data);
|
|
$mpdf->WriteHTML($html);
|
|
date_default_timezone_set('Asia/Kolkata');
|
|
$mpdf->Output($data['purchase'][0]->order_number .'_' . date('Y-m-d H-i-s') . '.pdf', 'D');
|
|
|
|
}
|
|
|
|
} |