Removed redundant form_types functions and the file /reporting/includes/form_types...
[fa-stable.git] / purchasing / includes / db / po_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4
5 function delete_po($po)
6 {
7         $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=" . $po;
8         db_query($sql, "The order header could not be deleted");
9
10         $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =" . $po;
11         db_query($sql, "The order detail lines could not be deleted");
12 }
13
14 //----------------------------------------------------------------------------------------
15
16 function add_po(&$po_obj)
17 {
18         begin_transaction();
19
20      /*Insert to purchase order header record */
21      $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES(";
22      $sql .= "'" . $po_obj->supplier_id . "', '" .
23          db_escape($po_obj->Comments) . "','" .
24          date2sql($po_obj->orig_order_date) . "', '" .
25                  $po_obj->reference . "', '" .
26          $po_obj->requisition_no . "', '" .
27          $po_obj->Location . "', '" .
28          $po_obj->delivery_address . "')";
29
30         db_query($sql, "The purchase order header record could not be inserted");
31
32      /*Get the auto increment value of the order number created from the sql above */
33      $po_obj->order_no = db_insert_id();
34
35      /*Insert the purchase order detail records */
36      foreach ($po_obj->line_items as $po_line)
37      {
38         if ($po_line->Deleted == false)
39         {
40                 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date,    unit_price,     quantity_ordered) VALUES (";
41                 $sql .= $po_obj->order_no . ", '" . $po_line->stock_id . "','" .
42                         $po_line->item_description . "','" .
43                         date2sql($po_line->req_del_date) . "'," .
44                         $po_line->price . ", " .
45                         $po_line->quantity . ")";
46                         db_query($sql, "One of the purchase order detail records could not be inserted");
47         }
48      }
49
50         references::save_last($po_obj->reference, systypes::po());
51
52         //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
53
54         commit_transaction();
55
56         return $po_obj->order_no;
57 }
58
59 //----------------------------------------------------------------------------------------
60
61 function update_po(&$po_obj)
62 {
63         begin_transaction();
64
65     /*Update the purchase order header with any changes */
66     $sql = "UPDATE ".TB_PREF."purch_orders SET Comments='" . db_escape($po_obj->Comments) . "',
67                 requisition_no= '" . $po_obj->requisition_no . "',
68                 into_stock_location='" . $po_obj->Location . "',
69                 ord_date='" . date2sql($po_obj->orig_order_date) . "',
70                 delivery_address='" . $po_obj->delivery_address . "'";
71     $sql .= " WHERE order_no = " . $po_obj->order_no;
72         db_query($sql, "The purchase order could not be updated");
73
74     /*Now Update the purchase order detail records */
75     foreach ($po_obj->line_items as $po_line)
76     {
77
78                 if ($po_line->Deleted==True)
79                 {
80                         // Sherifoz 21.06.03 Handle deleting existing lines
81                         if ($po_line->po_detail_rec!='')
82                         {
83                                 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE po_detail_item='" . $po_line->po_detail_rec . "'";
84                                 db_query($sql, "could not query purch order details");
85                         }
86                 }
87                 else if ($po_line->po_detail_rec == '')
88                 {
89                         // Sherifoz 21.06.03 Handle adding new lines vs. updating. if no key(po_detail_rec) then it's a new line
90                         $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price,        quantity_ordered) VALUES (";
91                         $sql .= $po_obj->order_no . ", '" .
92                                 $po_line->stock_id . "','" .
93                                 $po_line->item_description . "','" .
94                                 date2sql($po_line->req_del_date) . "'," .
95                                 $po_line->price . ", " . $po_line->quantity . ")";
96                 }
97                 else
98                 {
99                         $sql = "UPDATE ".TB_PREF."purch_order_details SET item_code='" . $po_line->stock_id . "',
100                                 description ='" . $po_line->item_description . "',
101                                 delivery_date ='" . date2sql($po_line->req_del_date) . "',
102                                 unit_price=" . $po_line->price . ",
103                                 quantity_ordered=" . $po_line->quantity . "
104                                 WHERE po_detail_item=" . $po_line->po_detail_rec;
105                 }
106                 db_query($sql, "One of the purchase order detail records could not be updated");
107     }
108
109         //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
110
111         commit_transaction();
112
113         return $po_obj->order_no;
114 }
115
116 //----------------------------------------------------------------------------------------
117
118 function read_po_header($order_no, &$order)
119 {
120         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
121                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
122                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
123                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
124                 AND ".TB_PREF."locations.loc_code = into_stock_location
125                 AND ".TB_PREF."purch_orders.order_no = " . $order_no;
126
127         $result = db_query($sql, "The order cannot be retrieved");
128
129         if (db_num_rows($result) == 1)
130         {
131
132         $myrow = db_fetch($result);
133
134         $order->order_no = $order_no;
135         $order->supplier_id = $myrow["supplier_id"];
136         $order->supplier_name = $myrow["supp_name"];
137         $order->curr_code = $myrow["curr_code"];
138
139         $order->orig_order_date = sql2date($myrow["ord_date"]);
140         $order->Comments = $myrow["comments"];
141         $order->Location = $myrow["into_stock_location"];
142         $order->requisition_no = $myrow["requisition_no"];
143         $order->reference = $myrow["reference"];
144         $order->delivery_address = $myrow["delivery_address"];
145
146         return true;
147         }
148
149         display_db_error("FATAL : duplicate purchase order found", "", true);
150         return false;
151 }
152
153 //----------------------------------------------------------------------------------------
154
155 function read_po_items($order_no, &$order, $open_items_only=false)
156 {
157         /*now populate the line po array with the purchase order details records */
158
159         $sql = "SELECT ".TB_PREF."purch_order_details.*, units
160                 FROM ".TB_PREF."purch_order_details
161                 LEFT JOIN ".TB_PREF."stock_master
162                 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
163                 WHERE order_no =$order_no ";
164
165     if ($open_items_only)
166                 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
167
168         $sql .= " ORDER BY po_detail_item";
169
170         $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
171
172     if (db_num_rows($result) > 0)
173     {
174
175                 while ($myrow = db_fetch($result))
176         {
177             if (is_null($myrow["units"]))
178             {
179                         $units = "";
180             }
181             else
182             {
183                 $units = $myrow["units"];
184             }
185
186             $order->add_to_order($order->lines_on_order+1, $myrow["item_code"],
187                 $myrow["quantity_ordered"],$myrow["description"],
188                 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
189                 $myrow["qty_invoiced"], $myrow["quantity_received"]);
190
191             $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
192             $order->line_items[$order->lines_on_order]->standard_cost = $myrow["std_cost_unit"];  /*Needed for receiving goods and GL interface */
193         } /* line po from purchase order details */
194     } //end of checks on returned data set
195 }
196
197 //----------------------------------------------------------------------------------------
198
199 function read_po($order_no, &$order, $open_items_only=false)
200 {
201         $result = read_po_header($order_no, $order);
202
203         if ($result)
204                 read_po_items($order_no, $order, $open_items_only);
205 }
206
207 //----------------------------------------------------------------------------------------
208
209
210 ?>