Advertisement
Guest User

Sale_m.php

a guest
Apr 8th, 2020
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.15 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class Sale_m extends CI_Model {
  4.  
  5.     function invoice_no()
  6.     {
  7.         $sql = "SELECT MAX(MID(invoice,9,4)) AS invoice_no
  8.                FROM t_sale
  9.                WHERE MID(invoice,3,6) = DATE_FORMAT(CURDATE(), '%y%m%d')";
  10.         $query = $this->db->query($sql);
  11.         if($query->num_rows() > 0) {
  12.             $row = $query->row();
  13.             $n = ((int)$row->invoice_no) + 1;
  14.             $no = sprintf("%'.04d", $n);
  15.         } else {
  16.             $no = "0001";
  17.         }
  18.         $invoice = "MP".date('ymd').$no;
  19.         return $invoice;
  20.     }
  21.  
  22.     public function get_cart($params = null)
  23.     {
  24.         $this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
  25.         $this->db->from('t_cart');
  26.         $this->db->join('p_item', 't_cart.item_id = p_item.item_id');
  27.         if($params != null) {
  28.             $this->db->where($params);
  29.         }
  30.         $this->db->where('user_id', $this->session->userdata('userid'));
  31.         $query = $this->db->get();
  32.         return $query;
  33.     }
  34.  
  35.     public function add_cart($post)
  36.     {
  37.         $query = $this->db->query("SELECT MAX(cart_id) AS cart_no FROM t_cart");
  38.         if($query->num_rows() > 0) {
  39.             $row = $query->row();
  40.             $car_no = ((int)$row->cart_no) + 1;
  41.         } else {
  42.             $car_no = "1";
  43.         }
  44.  
  45.         $params = array(
  46.             'cart_id' => $car_no,
  47.             'item_id' => $post['item_id'],
  48.             'price' => $post['price'],
  49.             'qty' => $post['qty'],
  50.             'total' => ($post['price'] * $post['qty']),
  51.             'user_id' => $this->session->userdata('userid')
  52.         );
  53.         $this->db->insert('t_cart', $params);
  54.     }
  55.  
  56.     function update_cart_qty($post) {
  57.         $sql = "UPDATE t_cart SET price = '$post[price]',
  58.                qty = qty + '$post[qty]',
  59.                total = '$post[price]' * qty
  60.                WHERE item_id = '$post[item_id]'";
  61.         $this->db->query($sql);
  62.     }
  63.  
  64.     public function del_cart($params = null)
  65.     {
  66.         if($params != null) {
  67.             $this->db->where($params);
  68.         }
  69.         $this->db->delete('t_cart');
  70.     }
  71.  
  72.     public function edit_cart($post)
  73.     {
  74.         $params = array(
  75.             'price' => $post['price'],
  76.             'qty' => $post['qty'],
  77.             'discount_item' => $post['discount'],
  78.             'total' => $post['total'],
  79.         );
  80.         $this->db->where('cart_id', $post['cart_id']);
  81.         $this->db->update('t_cart', $params);
  82.     }
  83.  
  84.  
  85.     public function add_sale($post)
  86.     {
  87.         $params = array(
  88.             'invoice' => $this->invoice_no(),
  89.             'customer_id' => $post['customer_id'] == "" ? null : $data['customer_id'],
  90.             'total_price' => $post['subtotal'],
  91.             'discount' => $post['discount'],
  92.             'final_price' => $post['grandtotal'],
  93.             'cash' => $post['cash'],
  94.             'remaining' => $post['change'],
  95.             'note' => $post['note'],
  96.             'date' => $post['date'],
  97.             'user_id' => $this->session->userdata('userid')
  98.         );
  99.         $this->db->insert('t_sale', $params);
  100.         return $this->db->insert_id();
  101.     }
  102.     function add_sale_detail($params) {
  103.         $this->db->insert_batch('t_sale_detail', $params);
  104.     }
  105.  
  106.     // public function get_sale($id = null)
  107.     // {
  108.     //  $this->db->select('*, customer.name as customer_name, user.username as user_name,
  109.     //                  t_sale.created as sale_created');
  110.     //  $this->db->from('t_sale');
  111.     //  $this->db->join('customer', 't_sale.customer_id = customer.customer_id', 'left');
  112.     //  $this->db->join('user', 't_sale.user_id = user.user_id');
  113.     //  if($id != null) {
  114.     //      $this->db->where('sale_id', $id);
  115.     //  }
  116.     //  $query = $this->db->get();
  117.     //  return $query;
  118.     // }
  119.     // public function get_sale_detail($sale_id = null)
  120.     // {
  121.     //  $this->db->from('t_sale_detail');
  122.     //  $this->db->join('p_item', 't_sale_detail.item_id = p_item.item_id');       
  123.     //  if($sale_id != null) {
  124.     //      $this->db->where('t_sale_detail.sale_id', $sale_id);
  125.     //  }
  126.     //  $query = $this->db->get();
  127.     //  return $query;
  128.     // }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement