3 // $bo_policy = 0, cancel remaining quantities on order
4 // = 1, backorder remaining quantities
6 function add_sales_invoice(&$invoice, $date_, $due_date, $sales_order, $tax_group_id,
7 $charge_shipping, $location, $ship_via, $sales_type, $reference, $memo_, $bo_policy)
11 $company_data = get_company_prefs();
13 $branch_data = get_branch_accounts($invoice->Branch);
15 $invoice_items_total = $invoice->get_items_total_dispatch($tax_group_id);
18 $taxes = $invoice->get_taxes($tax_group_id, $charge_shipping);
19 foreach ($taxes as $taxitem)
20 $tax_total += $taxitem['Value'];
22 /*Now insert the debtor_trans */
24 $invoice_no = add_customer_trans(10, $invoice->customer_id, $invoice->Branch, $date_,
25 $reference, $invoice_items_total, 0, $tax_total, $charge_shipping,
26 $sales_type, $sales_order, $ship_via, $due_date);
28 // If balance of the order cancelled update sales order details quantity.
29 foreach ($invoice->line_items as $order_line)
32 if ($order_line->qty_dispatched != 0)
35 $line_taxfree_price = $order_line->taxfree_price($tax_group_id);
36 $line_tax = $order_line->full_price() - $line_taxfree_price;
38 // Now update sales_order_details for the quantity invoiced
39 dispatch_sales_order_item($sales_order, $order_line->id,
40 $order_line->qty_dispatched);
42 /* add invoice details and stock movements */
45 add_customer_trans_detail_item(10, $invoice_no, $order_line->stock_id,
46 $order_line->item_description, $location, $date_,
47 -$order_line->qty_dispatched, $line_taxfree_price, $line_tax,
48 $order_line->discount_percent, $memo_, $order_line->standard_cost);
50 $stock_gl_code = get_stock_gl_code($order_line->stock_id);
52 /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
53 if ($order_line->standard_cost != 0)
56 /*first the cost of sales entry*/
57 add_gl_trans_std_cost(10, $invoice_no, $date_, $stock_gl_code["cogs_account"],
58 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], "",
59 $order_line->standard_cost * $order_line->qty_dispatched,
60 payment_person_types::customer(), $invoice->customer_id,
61 "The cost of sales GL posting could not be inserted");
63 /*now the stock entry*/
64 //$stock_gl_code = get_stock_gl_code($order_line->stock_id);
66 add_gl_trans_std_cost(10, $invoice_no, $date_, $stock_gl_code["inventory_account"], 0, 0, "",
67 (-$order_line->standard_cost * $order_line->qty_dispatched),
68 payment_person_types::customer(), $invoice->customer_id,
69 "The stock side of the cost of sales GL posting could not be inserted");
71 } /* end of if GL and stock integrated and standard cost !=0 */
73 if ($order_line->price != 0)
76 //Post sales transaction to GL credit sales
78 add_gl_trans_customer(10, $invoice_no, $date_, $stock_gl_code["sales_account"],
79 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
80 (-$line_taxfree_price * $order_line->qty_dispatched),
81 $invoice->customer_id, "The sales price GL posting could not be inserted");
83 if ($order_line->discount_percent != 0)
86 add_gl_trans_customer(10, $invoice_no, $date_,
87 $branch_data["sales_discount_account"], $stock_gl_code["dimension_id"],
88 $stock_gl_code["dimension2_id"],
89 ($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
90 $invoice->customer_id, "The sales discount GL posting could not be inserted");
91 } /*end of if discount !=0 */
92 } /*end of if sales integrated with debtors */
94 } /*quantity dispatched is more than 0 */
95 } /*end of order_line loop */
100 // if cancelling any remaining quantities
101 close_sales_order($sales_order);
104 /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
105 if (($invoice_items_total + $charge_shipping + $tax_total) != 0)
108 add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
109 ($invoice_items_total + $charge_shipping + $tax_total),
110 $invoice->customer_id, "The total debtor GL posting could not be inserted");
113 if ($charge_shipping != 0)
116 add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
117 (-$charge_shipping), $invoice->customer_id,
118 "The freight GL posting could not be inserted");
121 foreach ($taxes as $taxitem)
123 if ($taxitem['Value'] != 0)
125 add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
126 $taxitem['rate'], $taxitem['included_in_price'], $taxitem['Value']);
128 add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
129 (-$taxitem['Value']), $invoice->customer_id,
130 "A tax GL posting could not be inserted");
134 add_comments(10, $invoice_no, $date_, $memo_);
136 add_forms_for_sys_type(10, $invoice_no, $location);
138 references::save_last($reference, 10);
140 commit_transaction();
145 //--------------------------------------------------------------------------------------------------
147 function void_sales_invoice($type, $type_no)
151 void_bank_trans($type, $type_no, true);
152 void_gl_trans($type, $type_no, true);
154 // for invoices and credits related to invoices,
155 // reverse all the changes in the sales order
156 $items_result = get_customer_trans_details($type, $type_no);
158 $order = get_customer_trans_order($type, $type_no);
162 while ($row = db_fetch($items_result))
164 dispatch_sales_order_item($order, $row["id"], $row["quantity"]);
168 // clear details after they've been reversed in the sales order
169 void_customer_trans_details($type, $type_no);
171 void_customer_trans_tax_details($type, $type_no);
173 void_cust_allocations($type, $type_no);
175 // do this last because other voidings can depend on it - especially voiding
176 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
177 void_customer_trans($type, $type_no);
179 commit_transaction();
182 //--------------------------------------------------------------------------------------------------