1<?php
2/**
3 * Author: Ashish Shukla <gmail.com!wahjava>
4 *
5 * Script to duplicate BoMs.
6 */
7
8include('includes/session.php');
9
10$Title = _('Copy a BOM to New Item Code');
11
12include('includes/header.php');
13
14include('includes/SQL_CommonFunctions.inc');
15
16if(isset($_POST['Submit'])) {
17	$StockID = $_POST['StockID'];
18	$NewOrExisting = $_POST['NewOrExisting'];
19	$NewStockID = '';
20	$InputError = 0; //assume the best
21
22	if($NewOrExisting == 'N') {
23		$NewStockID = $_POST['ToStockID'];
24		if (mb_strlen($NewStockID)==0 OR $NewStockID==''){
25			$InputError = 1;
26			prnMsg(_('The new item code cannot be blank. Enter a new code for the item to copy the BOM to'),'error');
27		}
28	} else {
29		$NewStockID = $_POST['ExStockID'];
30	}
31	if ($InputError==0){
32		$result = DB_Txn_Begin();
33
34		if($NewOrExisting == 'N') {
35	      /* duplicate rows into stockmaster */
36			$sql = "INSERT INTO stockmaster( stockid,
37									categoryid,
38									description,
39									longdescription,
40									units,
41									mbflag,
42									actualcost,
43									lastcost,
44									materialcost,
45									labourcost,
46									overheadcost,
47									lowestlevel,
48									discontinued,
49									controlled,
50									eoq,
51									volume,
52									grossweight,
53									barcode,
54									discountcategory,
55									taxcatid,
56									serialised,
57									perishable,
58									digitals,
59									nextserialno,
60									pansize,
61									shrinkfactor,
62									netweight )
63							SELECT '".$NewStockID."' AS stockid,
64									categoryid,
65									description,
66									longdescription,
67									units,
68									mbflag,
69									actualcost,
70									lastcost,
71									materialcost,
72									labourcost,
73									overheadcost,
74									lowestlevel,
75									discontinued,
76									controlled,
77									eoq,
78									volume,
79									grossweight,
80									barcode,
81									discountcategory,
82									taxcatid,
83									serialised,
84									perishable,
85									digitals,
86									nextserialno,
87									pansize,
88									shrinkfactor,
89									netweight
90							FROM stockmaster
91							WHERE stockid='".$StockID."';";
92			$result = DB_query($sql);
93		} else {
94			$sql = "SELECT lastcostupdate,
95							actualcost,
96							lastcost,
97							materialcost,
98							labourcost,
99							overheadcost,
100							lowestlevel
101						FROM stockmaster
102						WHERE stockid='".$StockID."';";
103			$result = DB_query($sql);
104
105			$myrow = DB_fetch_row($result);
106
107			$sql = "UPDATE stockmaster set
108					lastcostupdate  = '" . $myrow[0] . "',
109					actualcost      = " . $myrow[1] . ",
110					lastcost        = " . $myrow[2] . ",
111					materialcost    = " . $myrow[3] . ",
112					labourcost      = " . $myrow[4] . ",
113					overheadcost    = " . $myrow[5] . ",
114					lowestlevel     = " . $myrow[6] . "
115					WHERE stockid='".$NewStockID."';";
116			$result = DB_query($sql);
117		}
118
119		$sql = "INSERT INTO bom
120					SELECT '".$NewStockID."' AS parent,
121					        sequence,
122							component,
123							workcentreadded,
124							loccode,
125							effectiveafter,
126							effectiveto,
127							quantity,
128							autoissue,
129							remark,
130							digitals
131					FROM bom
132					WHERE parent='".$StockID."';";
133		$result = DB_query($sql);
134
135		if($NewOrExisting == 'N') {
136			$sql = "INSERT INTO locstock (loccode,
137								            stockid,
138								            quantity,
139								            reorderlevel,
140								            bin )
141				      SELECT loccode,
142							'".$NewStockID."' AS stockid,
143							0 AS quantity,
144							reorderlevel,
145							bin
146						FROM locstock
147						WHERE stockid='".$StockID."'";
148
149			$result = DB_query($sql);
150		}
151
152		$result = DB_Txn_Commit();
153
154		UpdateCost($NewStockID);
155
156		header('Location: BOMs.php?Select='.$NewStockID);
157		ob_end_flush();
158	} //end  if there is no input error
159} else {
160
161	echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/inventory.png" title="' . _('Contract') . '" alt="" />' . ' ' . $Title . '</p>';
162
163	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '">';
164    echo '<div>';
165	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
166
167	$sql = "SELECT stockid,
168					description
169				FROM stockmaster
170				WHERE stockid IN (SELECT DISTINCT parent FROM bom)
171				AND  mbflag IN ('M', 'A', 'K', 'G');";
172	$result = DB_query($sql);
173
174	echo '<table class="selection">
175			<tr>
176				<td>' . _('From Stock ID') . '</td>';
177	echo '<td><select name="StockID">';
178	while($myrow = DB_fetch_row($result)) {
179		echo '<option value="'.$myrow[0].'">' . $myrow[0].' -- '.$myrow[1] . '</option>';
180	}
181	echo '</select></td>
182			</tr>';
183	echo '<tr>
184			<td><input type="radio" name="NewOrExisting" value="N" />' . _(' To New Stock ID') . '</td>';
185	echo '<td><input type="text" maxlength="20" autofocus="autofocus" pattern="[a-zA-Z0-9_\-]*" name="ToStockID" title="' . _('Enter a new item code to copy the existing item and its bill of material to. Item codes can contain only alpha-numeric characters, underscore or hyphens.') . '" /></td></tr>';
186
187	$sql = "SELECT stockid,
188					description
189				FROM stockmaster
190				WHERE stockid NOT IN (SELECT DISTINCT parent FROM bom)
191				AND mbflag IN ('M', 'A', 'K', 'G');";
192	$result = DB_query($sql);
193
194	if (DB_num_rows($result) > 0) {
195		echo '<tr>
196				<td><input type="radio" name="NewOrExisting" checked="checked" value="E" />' . _('To Existing Stock ID') . '</td><td>';
197		echo '<select name="ExStockID">';
198		while($myrow = DB_fetch_row($result)) {
199			echo '<option value="'.$myrow[0].'">' . $myrow[0].' -- '.$myrow[1] . '</option>';
200		}
201		echo '</select></td></tr>';
202	}
203	echo '</table>';
204	echo '<br /><div class="centre"><input type="submit" name="Submit" value="Submit" /></div>
205          </div>
206          </form>';
207
208	include('includes/footer.php');
209}
210?>
211