forked from daN4cat/PHP-Point-Of-Sale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsale.php
More file actions
executable file
·181 lines (156 loc) · 6.45 KB
/
sale.php
File metadata and controls
executable file
·181 lines (156 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class Sale extends Model
{
public function get_info($sale_id)
{
$this->db->from('sales');
$this->db->where('sale_id',$sale_id);
return $this->db->get();
}
function exists($sale_id)
{
$this->db->from('sales');
$this->db->where('sale_id',$sale_id);
$query = $this->db->get();
return ($query->num_rows()==1);
}
function save ($items,$customer_id,$employee_id,$comment,$payments,$sale_id=false)
{
$this->load->library('sale_lib');
if(count($items)==0)
return -1;
//Alain Multiple payments
//Build payment types string
$payment_types='';
foreach($payments as $payment_id=>$payment)
{
$payment_types=$payment_types.$payment['payment_type'].': '.to_currency($payment['payment_amount']).'<br>';
}
$sales_data = array(
'sale_time' => date('Y-m-d H:i:s'),
'customer_id'=> $this->Customer->exists($customer_id) ? $customer_id : null,
'employee_id'=>$employee_id,
'payment_type'=>$payment_types,
'comment'=>$comment
);
//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
$this->db->insert('sales',$sales_data);
$sale_id = $this->db->insert_id();
foreach($payments as $payment_id=>$payment)
{
$sales_payments_data = array
(
'sale_id'=>$sale_id,
'payment_type'=>$payment['payment_type'],
'payment_amount'=>$payment['payment_amount']
);
$this->db->insert('sales_payments',$sales_payments_data);
}
$line_items = array();
foreach($items as $line=>$item)
{
$cur_item_info = $this->Item->get_info($item['item_id']);
$sales_items_data = array
(
'sale_id'=>$sale_id,
'item_id'=>$item['item_id'],
'line'=>$item['line'],
'description'=>$item['description'],
'serialnumber'=>$item['serialnumber'],
'quantity_purchased'=>$item['quantity'],
'discount_percent'=>$item['discount'],
'item_cost_price' => $cur_item_info->cost_price,
'item_unit_price'=>$item['price']
);
$line_items[] = array_merge(array('item_info'=>$this->Item->get_info($item['item_id'])), $sales_items_data);
$this->db->insert('sales_items',$sales_items_data);
//Update stock quantity
$item_data = array('quantity'=>$cur_item_info->quantity - $item['quantity']);
$this->Item->save($item_data,$item['item_id']);
//Ramel Inventory Tracking
//Inventory Count Details
$qty_buy = -$item['quantity'];
$sale_remarks ='POS '.$sale_id;
$inv_data = array
(
'trans_date'=>date('Y-m-d H:i:s'),
'trans_items'=>$item['item_id'],
'trans_user'=>$employee_id,
'trans_comment'=>$sale_remarks,
'trans_inventory'=>$qty_buy
);
$this->Inventory->insert($inv_data);
//------------------------------------Ramel
$customer = $this->Customer->get_info($customer_id);
if ($customer_id == -1 or $customer->taxable)
{
foreach($this->Item_taxes->get_info($item['item_id']) as $row)
{
$this->db->insert('sales_items_taxes', array(
'sale_id' =>$sale_id,
'item_id' =>$item['item_id'],
'line' =>$item['line'],
'name' =>$row['name'],
'percent' =>$row['percent']
));
}
}
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
return -1;
}
postToQuickbooks('sale_add', array('sales_data'=>array_merge(array('sale_id'=>$sale_id), $sales_data), 'taxes' => $this->sale_lib->get_taxes(), 'payments' => $payments,'customer_info'=>$this->Customer->get_info($customer_id), 'line_items' =>$line_items));
return $sale_id;
}
function get_sale_items($sale_id)
{
$this->db->from('sales_items');
$this->db->where('sale_id',$sale_id);
return $this->db->get();
}
function get_sale_payments($sale_id)
{
$this->db->from('sales_payments');
$this->db->where('sale_id',$sale_id);
return $this->db->get();
}
function get_customer($sale_id)
{
$this->db->from('sales');
$this->db->where('sale_id',$sale_id);
return $this->Customer->get_info($this->db->get()->row()->customer_id);
}
//We create a temp table that allows us to do easy report/sales queries
public function create_sales_items_temp_table()
{
$this->db->query("CREATE TEMPORARY TABLE ".$this->db->dbprefix('sales_items_temp')."
(SELECT date(sale_time) as sale_date, ".$this->db->dbprefix('sales_items').".sale_id, comment,payment_type, customer_id, employee_id,
".$this->db->dbprefix('items').".item_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, SUM(percent) as item_tax_percent,
discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal,
".$this->db->dbprefix('sales_items').".line as line, serialnumber, ".$this->db->dbprefix('sales_items').".description as description,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(1+(SUM(percent)/100)),2) as total,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(percent)/100),2) as tax,
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit
FROM ".$this->db->dbprefix('sales_items')."
INNER JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'."
INNER JOIN ".$this->db->dbprefix('items')." ON ".$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('items').'.item_id'."
LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'."
LEFT OUTER JOIN ".$this->db->dbprefix('sales_items_taxes')." ON "
.$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." and "
.$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('sales_items_taxes').'.item_id'." and "
.$this->db->dbprefix('sales_items').'.line='.$this->db->dbprefix('sales_items_taxes').'.line'."
GROUP BY sale_id, item_id, line)");
//Update null item_tax_percents to be 0 instead of null
$this->db->where('item_tax_percent IS NULL');
$this->db->update('sales_items_temp', array('item_tax_percent' => 0));
//Update null tax to be 0 instead of null
$this->db->where('tax IS NULL');
$this->db->update('sales_items_temp', array('tax' => 0));
//Update null subtotals to be equal to the total as these don't have tax
$this->db->query('UPDATE '.$this->db->dbprefix('sales_items_temp'). ' SET total=subtotal WHERE total IS NULL');
}
}
?>