Huge sales module changes toward delivery and invoicing separation. Includes some...
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
1 <?php
2 function set_invoice_links(&$cart, $invoice) {
3     if(count($cart->trans_no)==1) {
4           
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');    
10
11           foreach($cart->line_items as $line) {
12             if($line->quantity!=$line->qty_dispatched) {
13                 // this is partial invoice
14                 return 1;
15             }
16           }
17         
18         }
19
20     $sql = 'UPDATE debtor_trans SET trans_link = ' . $invoice .
21         ' WHERE type=13 AND (';
22
23     $deliveries = $cart->trans_no;
24         
25     foreach($deliveries as $key=>$del) 
26             $deliveries[$key] = 'trans_no='.$del;
27     $sql .= implode(' OR ', $deliveries) . ')';
28     
29     db_query($sql, 'Delivery links cannot be updated');
30  return 0; // batch or complete invoice
31 }
32
33 function add_sales_invoice(&$cart, $date_, $due_date, $tax_group_id, 
34         $charge_shipping, $location, $ship_via, $sales_type, $reference, $memo_)
35 {
36         begin_transaction();
37         
38         $company_data = get_company_prefs();
39         
40         $branch_data = get_branch_accounts($cart->Branch);      
41         
42         $cart_items_total = $cart->get_items_total_dispatch($tax_group_id);
43
44         $delivery_no = $cart->trans_no;
45         if(is_array($delivery_no)) $delivery_no = 0;
46     
47     $tax_total = 0;
48     $taxes = $cart->get_taxes($tax_group_id, $charge_shipping);
49     foreach ($taxes as $taxitem)
50         $tax_total +=  $taxitem['Value'];
51
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
55
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);
59
60         set_invoice_links($cart,$invoice_no);
61         
62         // If balance of the order cancelled update sales order details quantity. 
63         foreach ($cart->line_items as $delivery_line) 
64         {
65
66                 if ($delivery_line->qty_dispatched != 0) 
67                 {
68                         
69                         $line_taxfree_price = $delivery_line->taxfree_price($tax_group_id);
70                         $line_tax = $delivery_line->full_price() - $line_taxfree_price;
71                 
72                         // Now update delivery items for the quantity invoiced 
73                         invoice_delivery_item($delivery_line->id,$delivery_line->qty_dispatched);
74                         
75                         /* add invoice details and stock movements */
76
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);
81                                                                                                 
82                         $stock_gl_code = get_stock_gl_code($delivery_line->stock_id);                                                                                           
83
84                         if ($delivery_line->price != 0)
85                         {
86
87                                 //Post sales transaction to GL credit sales
88                                 
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");
93                                 
94                                 if ($delivery_line->discount_percent != 0)
95                                 {
96                                         
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 */
104
105                 } /*quantity dispatched is more than 0 */
106         } /*end of delivery_line loop */
107
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) 
110         {
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");                               
114         }
115
116         if ($charge_shipping != 0) 
117         {
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");                                
121         }
122         
123     foreach ($taxes as $taxitem) 
124     {
125         if ($taxitem['Value'] != 0) 
126         {
127                         add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
128                                 $taxitem['rate'], $taxitem['included_in_price'], $taxitem['Value']);                    
129                 
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");
133         }       
134     }   
135     
136         add_comments(10, $invoice_no, $date_, $memo_);    
137         
138         add_forms_for_sys_type(10, $invoice_no, $location);
139         
140         references::save_last($reference, 10);  
141         
142         commit_transaction();   
143         
144         return $invoice_no;
145 }
146
147 //--------------------------------------------------------------------------------------------------
148
149 function void_sales_invoice($type, $type_no)
150 {
151         begin_transaction();
152         
153         void_bank_trans($type, $type_no, true);
154         void_gl_trans($type, $type_no, true);
155         
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);
159         
160         $delivery = get_customer_trans_link($type, $type_no);
161         
162         if ($delivery) 
163         {
164                 while ($row = db_fetch($items_result))
165                 {
166                         invoice_delivery_item($row['id'], $row['quantity']);
167                 }
168         }
169         
170         // clear details after they've been reversed in the sales order
171         void_customer_trans_details($type, $type_no);
172         
173         void_customer_trans_tax_details($type, $type_no);       
174         
175         void_cust_allocations($type, $type_no);
176         
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);   
180         
181         commit_transaction();                   
182 }
183
184 //--------------------------------------------------------------------------------------------------
185 function invoice_delivery_item($id, $qty_dispatched) 
186 {
187         $sql = "UPDATE ".TB_PREF."debtor_trans_details ";
188
189         $sql .= "SET qty_done = qty_done - $qty_dispatched ";
190
191         $sql .= " WHERE id=$id";
192 //              AND debtor_trans_type = 13
193 //              AND debtor_trans_no = $delivery_no
194 //              AND stock_id = '$stock_id'";
195
196         db_query($sql, "The sales delivery detail record could not be updated");
197 }
198
199 ?>