ok
Direktori : /home2/selectio/www/mm-tailor-billing/app/models/ |
Current File : /home2/selectio/www/mm-tailor-billing/app/models/Expenses_model.php |
<?php if (!defined('BASEPATH')) { exit('No direct script access allowed'); } class Expenses_model extends CI_Model { public function __construct() { parent::__construct(); } public function addExpenseRowBased($date,$amount,$category,$sub_category,$notes,$created_by) { for($e=0;$e<count($date);$e++) { $this->db->insert('tec_expenses', ['date' => $date[$e], 'amount' => $amount[$e], 'category' => $category, 'sub_category' => $sub_category[$e], 'note' => $notes[$e], 'created_by' => $created_by]); } return true; } public function deleteExpense($id) { if ($this->db->delete('expenses', ['id' => $id])) { return true; } return false; } public function getExpenseByID($id) { $q = $this->db->get_where('expenses', ['id' => $id], 1); if ($q->num_rows() > 0) { return $q->row(); } return false; } public function getProductByID($id) { $q = $this->db->get_where('products', ['id' => $id], 1); if ($q->num_rows() > 0) { return $q->row(); } return false; } public function getPurchaseByID($id) { $q = $this->db->get_where('purchases', ['id' => $id], 1); if ($q->num_rows() > 0) { return $q->row(); } return false; } public function getStoreQuantity($product_id, $store_id) { $q = $this->db->get_where('product_store_qty', ['product_id' => $product_id, 'store_id' => $store_id], 1); if ($q->num_rows() > 0) { return $q->row(); } return false; } public function setStoreQuantity($product_id, $store_id, $quantity) { if ($store_qty = $this->getStoreQuantity($product_id, $store_id)) { $this->db->update('product_store_qty', ['quantity' => ($store_qty->quantity + $quantity)], ['product_id' => $product_id, 'store_id' => $store_id]); } else { $this->db->insert('product_store_qty', ['product_id' => $product_id, 'store_id' => $store_id, 'quantity' => $quantity]); } } public function updateExpense($id, $data = []) { if ($this->db->update('expenses', $data, ['id' => $id])) { return true; } return false; } public function updatePurchase($id, $data = null, $items = []) { $purchase = $this->getPurchaseByID($id); if ($purchase->received) { $oitems = $this->getAllPurchaseItems($id); foreach ($oitems as $oitem) { if ($product = $this->site->getProductByID($oitem->product_id)) { $this->setStoreQuantity($oitem->product_id, $purchase->store_id, (0 - $oitem->quantity)); } } } if ($this->db->update('purchases', $data, ['id' => $id]) && $this->db->delete('purchase_items', ['purchase_id' => $id])) { foreach ($items as $item) { $item['purchase_id'] = $id; if ($this->db->insert('purchase_items', $item)) { if ($data['received'] && $product = $this->site->getProductByID($item['product_id'])) { $this->setStoreQuantity($item['product_id'], $purchase->store_id, $item['quantity']); } } } return true; } return false; } }