3 //-----------------------------------------------------------------------------
4 // Add or update Sales Invoice
6 function write_sales_invoice(&$invoice)
9 $trans_no = $invoice->trans_no;
10 if (is_array($trans_no))
11 $trans_no = key($trans_no);
13 $date_ = $invoice->document_date;
14 $charge_shipping =$invoice->freight_cost;
18 $company_data = get_company_prefs();
20 $branch_data = get_branch_accounts($invoice->Branch);
22 // offer price values without freight costs
23 $items_total = $invoice->get_items_total_dispatch();
24 $freight_tax = $invoice->get_shipping_tax();
26 $delivery_no = $invoice->src_docs;
27 if (is_array($delivery_no))
30 update_customer_trans_version(get_parent_type(10), $invoice->src_docs);
33 $taxes = $invoice->get_taxes(); // all taxes with freight_tax
35 foreach ($taxes as $taxitem) {
36 $ov_gst += $taxitem['Value'];
39 if($invoice->tax_included==0) {
40 $items_added_tax = $ov_gst-$freight_tax;
41 $freight_added_tax = $freight_tax;
44 $freight_added_tax = 0;
47 /* Insert/update the debtor_trans */
48 $sales_order = $invoice->order_no;
49 if (is_array($sales_order))
50 $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
52 $invoice_no = write_customer_trans(10, $trans_no, $invoice->customer_id,
53 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
54 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
55 $invoice->default_sales_type, $sales_order, $delivery_no,
56 $invoice->ship_via, $invoice->due_date);
59 $invoice->trans_no = array($invoice_no=>0);
60 set_document_parent($invoice);
62 delete_comments(10, $invoice_no);
63 void_gl_trans(10, $invoice_no, true);
64 void_cust_allocations(10, $invoice_no); // ?
65 void_customer_trans_tax_details(10, $invoice_no);
68 foreach ($invoice->line_items as $invoice_line) {
70 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
71 $invoice_line->price, 0, $invoice->tax_included,
72 $invoice->tax_group_array);
74 $line_tax = get_full_price_for_item($invoice_line->stock_id,
75 $invoice_line->price, 0, $invoice->tax_included,
76 $invoice->tax_group_array) - $line_taxfree_price;
78 write_customer_trans_detail_item(10, $invoice_no, $invoice_line->stock_id,
79 $invoice_line->item_description, $invoice_line->qty_dispatched,
80 $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
81 $invoice_line->standard_cost,
82 $trans_no ? $invoice_line->id : 0);
84 // Update delivery items for the quantity invoiced
85 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
86 update_parent_line(10, $invoice_line->id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
88 if ($invoice_line->qty_dispatched != 0) {
89 $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
91 if ($invoice_line->line_price() != 0) {
92 //Post sales transaction to GL credit sales
94 add_gl_trans_customer(10, $invoice_no, $date_, $stock_gl_code["sales_account"],
95 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
96 (-$line_taxfree_price * $invoice_line->qty_dispatched),
97 $invoice->customer_id, "The sales price GL posting could not be inserted");
99 if ($invoice_line->discount_percent != 0) {
101 add_gl_trans_customer(10, $invoice_no, $date_,
102 $branch_data["sales_discount_account"], $stock_gl_code["dimension_id"],
103 $stock_gl_code["dimension2_id"],
104 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
105 $invoice->customer_id, "The sales discount GL posting could not be inserted");
106 } /*end of if discount !=0 */
108 } /*quantity dispatched is more than 0 */
109 } /*end of delivery_line loop */
111 /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
112 if (($items_total + $charge_shipping) != 0) {
113 add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
114 ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
115 $invoice->customer_id, "The total debtor GL posting could not be inserted");
117 if ($charge_shipping != 0) {
118 add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
119 -$invoice->get_tax_free_shipping(), $invoice->customer_id,
120 "The freight GL posting could not be inserted");
123 foreach ($taxes as $taxitem) {
124 if ($taxitem['Value'] != 0) {
125 add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
126 $taxitem['rate'], $invoice->tax_included, $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_, $invoice->Comments);
136 if ($trans_no == 0) {
137 references::save_last($invoice->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 $delivery = get_customer_trans_link($type, $type_no);
161 while ($row = db_fetch($items_result)) {
162 update_parent_line(10, $row['id'], -$row['quantity']);
166 // clear details after they've been reversed in the sales order
167 void_customer_trans_details($type, $type_no);
169 void_customer_trans_tax_details($type, $type_no);
171 void_cust_allocations($type, $type_no);
173 // do this last because other voidings can depend on it - especially voiding
174 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
175 void_customer_trans($type, $type_no);
177 commit_transaction();