1<?php
2/*Contract_Readin.php is used by the modify existing Contract in Contracts.php and also by ContractCosting.php */
3
4$ContractHeaderSQL = "SELECT contractdescription,
5							contracts.debtorno,
6							contracts.branchcode,
7							contracts.loccode,
8							contracts.customerref,
9							status,
10							categoryid,
11							orderno,
12							margin,
13							wo,
14							requireddate,
15							drawing,
16							exrate,
17							debtorsmaster.name,
18							custbranch.brname,
19							debtorsmaster.currcode
20						FROM contracts INNER JOIN debtorsmaster
21						ON contracts.debtorno=debtorsmaster.debtorno
22						INNER JOIN currencies
23						ON debtorsmaster.currcode=currencies.currabrev
24						INNER JOIN custbranch
25						ON debtorsmaster.debtorno=custbranch.debtorno
26						AND contracts.branchcode=custbranch.branchcode
27						INNER JOIN locationusers ON locationusers.loccode=contracts.loccode AND locationusers.userid='" .  $_SESSION['UserID'] . "' AND locationusers.canupd=1
28						WHERE contractref= '" . $ContractRef . "'";
29
30$ErrMsg =  _('The contract cannot be retrieved because');
31$DbgMsg =  _('The SQL statement that was used and failed was');
32$ContractHdrResult = DB_query($ContractHeaderSQL,$ErrMsg,$DbgMsg);
33
34if (DB_num_rows($ContractHdrResult)==1 and !isset($_SESSION['Contract'.$identifier]->ContractRef )) {
35
36	$myrow = DB_fetch_array($ContractHdrResult);
37	$_SESSION['Contract'.$identifier]->ContractRef = $ContractRef;
38	$_SESSION['Contract'.$identifier]->ContractDescription = $myrow['contractdescription'];
39	$_SESSION['Contract'.$identifier]->DebtorNo = $myrow['debtorno'];
40	$_SESSION['Contract'.$identifier]->BranchCode = $myrow['branchcode'];
41	$_SESSION['Contract'.$identifier]->LocCode = $myrow['loccode'];
42	$_SESSION['Contract'.$identifier]->CustomerRef = $myrow['customerref'];
43	$_SESSION['Contract'.$identifier]->Status = $myrow['status'];
44	$_SESSION['Contract'.$identifier]->CategoryID = $myrow['categoryid'];
45	$_SESSION['Contract'.$identifier]->OrderNo = $myrow['orderno'];
46	$_SESSION['Contract'.$identifier]->Margin = $myrow['margin'];
47	$_SESSION['Contract'.$identifier]->WO = $myrow['wo'];
48	$_SESSION['Contract'.$identifier]->RequiredDate = ConvertSQLDate($myrow['requireddate']);
49	$_SESSION['Contract'.$identifier]->Drawing = $myrow['drawing'];
50	$_SESSION['Contract'.$identifier]->ExRate = $myrow['exrate'];
51	$_SESSION['Contract'.$identifier]->BranchName = $myrow['brname'];
52	$_SESSION['RequireCustomerSelection'] = 0;
53	$_SESSION['Contract'.$identifier]->CustomerName = $myrow['name'];
54	$_SESSION['Contract'.$identifier]->CurrCode = $myrow['currcode'];
55
56
57/*now populate the contract BOM array with the items required for the contract */
58
59	$ContractBOMsql = "SELECT contractbom.stockid,
60							stockmaster.description,
61							contractbom.workcentreadded,
62							contractbom.quantity,
63							stockmaster.units,
64							stockmaster.decimalplaces,
65							stockmaster.materialcost+stockmaster.labourcost+stockmaster.overheadcost AS cost
66						FROM contractbom INNER JOIN stockmaster
67						ON contractbom.stockid=stockmaster.stockid
68						WHERE contractref ='" . $ContractRef . "'";
69
70	$ErrMsg =  _('The bill of material cannot be retrieved because');
71	$DbgMsg =  _('The SQL statement that was used to retrieve the contract bill of material was');
72	$ContractBOMResult = DB_query($ContractBOMsql,$ErrMsg,$DbgMsg);
73
74	if (DB_num_rows($ContractBOMResult) > 0) {
75		while ($myrow=DB_fetch_array($ContractBOMResult)) {
76			$_SESSION['Contract'.$identifier]->Add_To_ContractBOM($myrow['stockid'],
77																	$myrow['description'],
78																	$myrow['workcentreadded'],
79																	$myrow['quantity'],
80																	$myrow['cost'],
81																	$myrow['units'],
82																	$myrow['decimalplaces']);
83		} /* add contract bill of materials BOM lines*/
84	} //end is there was a contract BOM to add
85	//Now add the contract requirments
86	$ContractReqtsSQL = "SELECT requirement,
87								quantity,
88								costperunit,
89								contractreqid
90						FROM contractreqts
91						WHERE contractref ='" . $ContractRef . "'
92						ORDER BY contractreqid";
93
94	$ErrMsg =  _('The other contract requirementscannot be retrieved because');
95	$DbgMsg =  _('The SQL statement that was used to retrieve the other contract requirments was');
96	$ContractReqtsResult = DB_query($ContractReqtsSQL,$ErrMsg,$DbgMsg);
97
98	if (DB_num_rows($ContractReqtsResult) > 0) {
99		while ($myrow=DB_fetch_array($ContractReqtsResult)) {
100			$_SESSION['Contract'.$identifier]->Add_To_ContractRequirements($myrow['requirement'],
101																		   $myrow['quantity'],
102																		   $myrow['costperunit'],
103																		   $myrow['contractreqid']);
104		} /* add other contract requirments lines*/
105	} //end is there are contract other contract requirments to add
106} // end if there was a header for the contract
107?>