2 function set_invoice_links(&$cart, $invoice) {
3 if(count($cart->trans_no)==1) {
5 $del = $cart->trans_no;
6 if(is_array($del)) $del = $del[0];
7 $sql = 'UPDATE debtor_trans SET trans_link = ' . $del .
8 ' WHERE type=10 AND trans_no='.$invoice;
9 db_query($sql, 'Invoice link cannot be updated');
11 foreach($cart->line_items as $line) {
12 if($line->quantity!=$line->qty_dispatched) {
13 // this is partial invoice
20 $sql = 'UPDATE debtor_trans SET trans_link = ' . $invoice .
21 ' WHERE type=13 AND (';
23 $deliveries = $cart->trans_no;
25 foreach($deliveries as $key=>$del)
26 $deliveries[$key] = 'trans_no='.$del;
27 $sql .= implode(' OR ', $deliveries) . ')';
29 db_query($sql, 'Delivery links cannot be updated');
30 return 0; // batch or complete invoice
33 function add_sales_invoice(&$cart, $date_, $due_date, $tax_group_id,
34 $charge_shipping, $location, $ship_via, $sales_type, $reference, $memo_)
38 $company_data = get_company_prefs();
40 $branch_data = get_branch_accounts($cart->Branch);
42 $cart_items_total = $cart->get_items_total_dispatch($tax_group_id);
44 $delivery_no = $cart->trans_no;
45 if(is_array($delivery_no)) $delivery_no = 0;
48 $taxes = $cart->get_taxes($tax_group_id, $charge_shipping);
49 foreach ($taxes as $taxitem)
50 $tax_total += $taxitem['Value'];
52 /*Now insert the debtor_trans */
53 $sales_order=$cart->order_no;
54 if(is_array($sales_order)) $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
56 $invoice_no = add_customer_trans(10, $cart->customer_id, $cart->Branch, $date_,
57 $reference, $cart_items_total, 0, $tax_total, $charge_shipping,
58 $sales_type, $sales_order, $delivery_no, $ship_via, $due_date);
60 set_invoice_links($cart,$invoice_no);
62 // If balance of the order cancelled update sales order details quantity.
63 foreach ($cart->line_items as $delivery_line)
66 if ($delivery_line->qty_dispatched != 0)
69 $line_taxfree_price = $delivery_line->taxfree_price($tax_group_id);
70 $line_tax = $delivery_line->full_price() - $line_taxfree_price;
72 // Now update delivery items for the quantity invoiced
73 invoice_delivery_item($delivery_line->id,$delivery_line->qty_dispatched);
75 /* add invoice details and stock movements */
77 add_customer_trans_detail_item(10, $invoice_no, $delivery_line->stock_id,
78 $delivery_line->item_description, $location, $date_,
79 -$delivery_line->qty_dispatched, $line_taxfree_price, $line_tax,
80 $delivery_line->discount_percent, $reference, $delivery_line->standard_cost);
82 $stock_gl_code = get_stock_gl_code($delivery_line->stock_id);
84 if ($delivery_line->price != 0)
87 //Post sales transaction to GL credit sales
89 add_gl_trans_customer(10, $invoice_no, $date_, $stock_gl_code["sales_account"],
90 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
91 (-$line_taxfree_price * $delivery_line->qty_dispatched),
92 $cart->customer_id, "The sales price GL posting could not be inserted");
94 if ($delivery_line->discount_percent != 0)
97 add_gl_trans_customer(10, $invoice_no, $date_,
98 $branch_data["sales_discount_account"], $stock_gl_code["dimension_id"],
99 $stock_gl_code["dimension2_id"],
100 ($line_taxfree_price * $delivery_line->qty_dispatched * $delivery_line->discount_percent),
101 $cart->customer_id, "The sales discount GL posting could not be inserted");
102 } /*end of if discount !=0 */
103 } /*end of if sales integrated with debtors */
105 } /*quantity dispatched is more than 0 */
106 } /*end of delivery_line loop */
108 /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
109 if (($cart_items_total + $charge_shipping + $tax_total) != 0)
111 add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
112 ($cart_items_total + $charge_shipping + $tax_total),
113 $cart->customer_id, "The total debtor GL posting could not be inserted");
116 if ($charge_shipping != 0)
118 add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
119 (-$charge_shipping), $cart->customer_id,
120 "The freight GL posting could not be inserted");
123 foreach ($taxes as $taxitem)
125 if ($taxitem['Value'] != 0)
127 add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
128 $taxitem['rate'], $taxitem['included_in_price'], $taxitem['Value']);
130 add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
131 (-$taxitem['Value']), $cart->customer_id,
132 "A tax GL posting could not be inserted");
136 add_comments(10, $invoice_no, $date_, $memo_);
138 add_forms_for_sys_type(10, $invoice_no, $location);
140 references::save_last($reference, 10);
142 commit_transaction();
147 //--------------------------------------------------------------------------------------------------
149 function void_sales_invoice($type, $type_no)
153 void_bank_trans($type, $type_no, true);
154 void_gl_trans($type, $type_no, true);
156 // for invoices and credits related to invoices,
157 // reverse all the changes in the sales order
158 $items_result = get_customer_trans_details($type, $type_no);
160 $delivery = get_customer_trans_link($type, $type_no);
164 while ($row = db_fetch($items_result))
166 invoice_delivery_item($row['id'], $row['quantity']);
170 // clear details after they've been reversed in the sales order
171 void_customer_trans_details($type, $type_no);
173 void_customer_trans_tax_details($type, $type_no);
175 void_cust_allocations($type, $type_no);
177 // do this last because other voidings can depend on it - especially voiding
178 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
179 void_customer_trans($type, $type_no);
181 commit_transaction();
184 //--------------------------------------------------------------------------------------------------
185 function invoice_delivery_item($id, $qty_dispatched)
187 $sql = "UPDATE ".TB_PREF."debtor_trans_details ";
189 $sql .= "SET qty_done = qty_done - $qty_dispatched ";
191 $sql .= " WHERE id=$id";
192 // AND debtor_trans_type = 13
193 // AND debtor_trans_no = $delivery_no
194 // AND stock_id = '$stock_id'";
196 db_query($sql, "The sales delivery detail record could not be updated");