Moving 2.0 development version to main trunk.
[fa-stable.git] / manufacturing / includes / db / work_order_produce_items_db.inc
1 <?php
2
3 function work_order_produce($woid, $ref, $quantity, $date_, $memo_, $close_wo)
4 {
5         begin_transaction();
6
7         $details = get_work_order($woid);
8
9     if (strlen($details[0]) == 0)
10     {
11         echo _("The order number sent is not valid.");
12         exit;
13     }
14
15         if (work_order_is_closed($woid))
16         {
17                 display_error("UNEXPECTED : Producing Items for a closed Work Order");
18                 cancel_transaction();
19                 exit;
20         }
21
22     $date = date2sql($date_);
23
24     $sql = "INSERT INTO ".TB_PREF."wo_manufacture (workorder_id, reference, quantity, date_)
25                 VALUES ($woid, ".db_escape($ref).", $quantity, '$date')";
26
27         db_query($sql,"A work order manufacture could not be added");
28
29         $id = db_insert_id();
30
31         // insert a +ve stock move for the item being manufactured
32         // negative means "unproduce" or unassemble
33         add_stock_move(29, $details["stock_id"], $id,
34                 $details["loc_code"], $date_, $memo_, $quantity, 0);
35
36         // update wo quantity and close wo if requested
37         work_order_update_finished_quantity($woid, $quantity, $close_wo);
38
39         // -------------------------------------------------------------------------
40
41         work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, 0);
42
43         // -------------------------------------------------------------------------
44         if ($memo_)
45                 add_comments(29, $id, $date_, $memo_);
46
47         references::save_last($ref, 29);
48
49         commit_transaction();
50 }
51
52 //--------------------------------------------------------------------------------------------
53
54 function get_work_order_produce($id)
55 {
56         $sql = "SELECT ".TB_PREF."wo_manufacture.*,".TB_PREF."workorders.stock_id, ".TB_PREF."stock_master.description AS StockDescription
57                 FROM ".TB_PREF."wo_manufacture, ".TB_PREF."workorders, ".TB_PREF."stock_master
58                 WHERE ".TB_PREF."wo_manufacture.workorder_id=".TB_PREF."workorders.id
59                 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
60                 AND ".TB_PREF."wo_manufacture.id=$id";
61     $result = db_query($sql, "The work order production could not be retrieved");
62
63     return db_fetch($result);
64 }
65
66 //--------------------------------------------------------------------------------------
67
68 function get_work_order_productions($woid)
69 {
70         $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id=$woid ORDER BY id";
71     return db_query($sql, "The work order issues could not be retrieved");
72 }
73
74 //--------------------------------------------------------------------------------------
75
76 function exists_work_order_produce($id)
77 {
78         $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=$id";
79         $result = db_query($sql, "Cannot retreive a wo production");
80
81     return (db_num_rows($result) > 0);
82 }
83
84 //--------------------------------------------------------------------------------------------
85
86 function void_work_order_produce($type_no)
87 {
88         begin_transaction();
89
90         $row = get_work_order_produce($type_no);
91
92         // deduct the quantity of this production from the parent work order
93         work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
94
95         // clear the production record
96         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=$type_no";
97         db_query($sql, "Cannot void a wo production");
98
99         // void all related stock moves
100         void_stock_move(29, $type_no);
101
102         // void any related gl trans
103         void_gl_trans(29, $type_no, true);
104
105         commit_transaction();
106 }
107
108
109 ?>