2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //-----------------------------------------------------------------------------
13 // insert/update sales delivery
15 function write_sales_delivery(&$delivery,$bo_policy)
19 $trans_no = $delivery->trans_no;
20 if (is_array($trans_no)) $trans_no = key($trans_no);
23 $delivery->bo_policy = $bo_policy;
24 hook_db_prewrite($delivery, ST_CUSTDELIVERY);
26 $customer = get_customer($delivery->customer_id);
28 add_new_exchange_rate($customer['curr_code'], $delivery->document_date, $delivery->ex_rate);
30 $delivery_items_total = $delivery->get_items_total_dispatch();
31 $freight_tax = $delivery->get_shipping_tax();
33 // mark sales order for concurrency conflicts check
34 update_sales_order_version($delivery->src_docs);
37 $taxes = $delivery->get_taxes(); // all taxes with freight_tax
39 foreach ($taxes as $taxitem) {
40 $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
41 $tax_total += $taxitem['Value'];
43 /* Insert/update the debtor_trans */
44 $delivery_no = write_customer_trans(ST_CUSTDELIVERY, $trans_no, $delivery->customer_id,
45 $delivery->Branch, $delivery->document_date, $delivery->reference,
46 $delivery_items_total, 0,
47 $delivery->tax_included ? 0 : $tax_total-$freight_tax,
48 $delivery->freight_cost,
49 $delivery->tax_included ? 0 : $freight_tax,
50 $delivery->sales_type, $delivery->order_no,
51 $delivery->ship_via, $delivery->due_date, 0, 0, $delivery->dimension_id,
52 $delivery->dimension2_id, $delivery->payment, $delivery->tax_included, $delivery->prep_amount);
55 $delivery->trans_no = array($delivery_no=>0);
57 move_trans_attachments(ST_CUSTDELIVERY, $trans_no, $delivery_no);
58 void_gl_trans(ST_CUSTDELIVERY, $delivery_no, true);
59 void_stock_move(ST_CUSTDELIVERY, $delivery_no);
60 void_trans_tax_details(ST_CUSTDELIVERY, $delivery_no);
61 delete_comments(ST_CUSTDELIVERY, $delivery_no);
65 // for prepaid delivery repost income from deffered account
66 if ($delivery->is_prepaid())
68 $branch_data = get_branch_accounts($delivery->Branch);
72 $delivery->split_line_values();
74 foreach ($delivery->line_items as $line_no => $line) {
76 $qty = $line->qty_dispatched;
77 $line_price = $line->line_price();
78 $line_taxfree_price = $line->gl_amounts['Net'];
80 $line_tax = $line->gl_amounts['Tax'];
82 $line->unit_cost = get_unit_cost($line->stock_id);
84 /* add delivery details for all lines */
85 write_customer_trans_detail_item(ST_CUSTDELIVERY, $delivery_no, $line->stock_id,
86 $line->item_description, $line->qty_dispatched,
87 $line->line_price(), $qty ? $line_tax/$qty : 0,
88 $line->discount_percent, $line->unit_cost, $line->src_id,
89 $trans_no ? $line->id : 0);
91 // Now update sales_order_details for the quantity delivered
92 if ($line->qty_old != $line->qty_dispatched)
93 update_parent_line(ST_CUSTDELIVERY, $line->src_id,
94 $line->qty_dispatched-$line->qty_old);
96 if ($line->qty_dispatched != 0) {
97 add_stock_move(ST_CUSTDELIVERY, $line->stock_id, $delivery_no,
98 $delivery->Location, $delivery->document_date, $delivery->reference,
99 -$line->qty_dispatched, $line->unit_cost,
100 $line_price*(1-$line->discount_percent));
102 $mb_flag = get_mb_flag($line->stock_id);
104 if (is_fixed_asset($mb_flag)) {
105 $sql = "UPDATE ".TB_PREF."stock_master SET inactive=1, material_cost=0
106 WHERE stock_id=".db_escape($line->stock_id);
107 db_query($sql,"The cost details for the fixed asset could not be updated");
110 $stock_gl_code = get_stock_gl_code($line->stock_id);
112 // If there is a Customer Dimension, then override with this,
113 // else take the Item Dimension (if any)
114 $dim = ($delivery->dimension_id != $customer['dimension_id'] ? $delivery->dimension_id :
115 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
116 $dim2 = ($delivery->dimension2_id != $customer['dimension2_id'] ? $delivery->dimension2_id :
117 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
118 if ($delivery->is_prepaid())
120 $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
122 $total += add_gl_trans_customer(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $sales_account, $dim, $dim2,
123 -$line_taxfree_price, $delivery->customer_id);
125 /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
127 if (is_inventory_item($line->stock_id)) {
129 if ($delivery->fixed_asset) {
130 $fa_purchase_cost = get_purchase_cost($line->stock_id);
131 $fa_depreciation = $fa_purchase_cost - $line->unit_cost;
132 /*first remove depreciation*/
133 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
134 $delivery->document_date, $stock_gl_code["adjustment_account"], $dim, $dim2, "",
136 PT_CUSTOMER, $delivery->customer_id);
137 /*then remove asset*/
138 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
139 $stock_gl_code["inventory_account"], 0, 0, "",
141 PT_CUSTOMER, $delivery->customer_id);
142 /*finally adjust sales account with the remaining*/
143 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
144 $stock_gl_code["cogs_account"], 0, 0, "",
145 ($fa_purchase_cost - $fa_depreciation),
146 PT_CUSTOMER, $delivery->customer_id);
149 else if ($line->unit_cost != 0) {
150 /*first the cost of sales entry*/
151 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
152 $delivery->document_date, $stock_gl_code["cogs_account"], $dim, $dim2, "",
153 $line->unit_cost * $line->qty_dispatched,
154 PT_CUSTOMER, $delivery->customer_id);
156 /*now the stock entry*/
157 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
158 $stock_gl_code["inventory_account"], 0, 0, "",
159 (-$line->unit_cost * $line->qty_dispatched),
160 PT_CUSTOMER, $delivery->customer_id);
163 } /* end of if GL and stock integrated and standard cost !=0 */
165 } /*quantity dispatched is more than 0 */
166 } /*end of order_line loop */
168 if ($delivery->is_prepaid())
170 $deferred_act = get_company_pref('deferred_income_act');
171 add_gl_trans_customer(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $deferred_act, 0, 0,
172 -$total, $delivery->customer_id);
175 if ($bo_policy == 0) {
176 // if cancelling any remaining quantities
177 close_sales_order($delivery->order_no);
180 // taxes - this is for printing purposes
181 foreach ($taxes as $taxitem) {
182 if ($taxitem['Net'] != 0) {
183 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($delivery->customer_id), $delivery->document_date);
184 add_trans_tax_details(ST_CUSTDELIVERY, $delivery_no, $taxitem['tax_type_id'],
185 $taxitem['rate'], $delivery->tax_included, $taxitem['Value'],
186 $taxitem['Net'], $ex_rate, $delivery->document_date, $delivery->reference, null, $delivery->tax_group_id);
190 add_comments(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $delivery->Comments);
192 if ($trans_no == 0) {
193 $Refs->save(ST_CUSTDELIVERY, $delivery_no, $delivery->reference);
195 hook_db_postwrite($delivery, ST_CUSTDELIVERY);
196 commit_transaction();
201 //--------------------------------------------------------------------------------------------------
202 function adjust_shipping_charge(&$delivery, $trans_no)
204 $sql = "SELECT sum(ov_freight) as freight FROM ".TB_PREF."debtor_trans WHERE order_ = $trans_no AND type = " . ST_CUSTDELIVERY . " AND debtor_no = " . $delivery->customer_id;
205 $result = db_query($sql, "Can not find delivery notes");
206 $row = db_fetch_row($result);
207 if (!$row[0]) $freight = 0;
208 else $freight = $row[0];
209 if ($freight < $delivery->freight_cost) $delivery->freight_cost = $delivery->freight_cost - $freight;
210 else $delivery->freight_cost = 0;
213 //--------------------------------------------------------------------------------------------------
215 function void_sales_delivery($type, $type_no, $transactions=true)
220 hook_db_prevoid($type, $type_no);
222 void_gl_trans($type, $type_no, true);
224 // reverse all the changes in the sales order
225 $items_result = get_customer_trans_details($type, $type_no);
227 $order = get_customer_trans_order($type, $type_no);
230 $auto = (get_reference(ST_SALESORDER, $order) == "auto");
231 while ($row = db_fetch($items_result)) {
232 update_parent_line(ST_CUSTDELIVERY, $row['src_id'], -$row['quantity'], $auto);
236 // clear details after they've been reversed in the sales order
237 void_customer_trans_details($type, $type_no);
239 void_trans_tax_details($type, $type_no);
241 void_cust_allocations($type, $type_no);
243 // do this last because other voidings can depend on it
244 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
245 void_customer_trans($type, $type_no);
248 commit_transaction();