1<?php
2// DefineTenderClass.php
3// Definition of the tender class to hold all the information for a supplier tender.
4
5Class Tender {
6
7	var $TenderId;
8	var $LineItems; /*array of objects of class LineDetails using the product id as the pointer */
9	var $CurrCode;
10	var $ExRate;
11	var $Initiator;
12	var $RequiredByDate;
13	var $RequisitionNo;
14	var $DelAdd1;
15	var $DelAdd2;
16	var $DelAdd3;
17	var $DelAdd4;
18	var $DelAdd5;
19	var $DelAdd6;
20	var $Telephone;
21	var $Comments;
22	var $Location;
23	var $OrderNo; /*Only used for modification of existing orders otherwise only established when order committed */
24	var $LinesOnTender;
25	var $SuppliersOnTender;
26	var $GLLink; /*Is the GL link to stock activated only checked when order initiated or reading in for modification */
27	var $Version;
28	var $Revised;
29	var $contact;
30	var $Suppliers;
31
32	function __construct(){
33	/*Constructor function initialises a new purchase tender object */
34		$this->LineItems = array();
35		$this->Suppliers = array();
36		$this->LinesOnTender=0;
37		$this->SuppliersOnTender=0;
38	}
39
40	function Tender(){
41		self::__construct();
42	}
43
44	function EmailSuppliers() {
45		$EmailText= _('This email has been automatically generated by webERP') . "\n";
46		$EmailText.= _('You are invited to Tender for the following products to be delivered to') . ' ' . $_SESSION['CompanyRecord']['coyname'] . "\n";
47		$EmailText.= _('Tender number') . ': ' . $this->TenderId  . "\n";
48		$EmailText.= _('Quantity').'   '._('Unit').'   '._('Item Description')."\n";
49		foreach ($this->LineItems as $LineItem) {
50			$EmailText.= $LineItem->Quantity.' '.$LineItem->Units.' '.$LineItem->ItemDescription . "\n";
51		}
52		$Subject=(_('Tender received from').' '.$_SESSION['CompanyRecord']['coyname'] );
53		$Headers = 'From: '. $_SESSION['PurchasingManagerEmail']. "\r\n" . 'Reply-To: ' . $_SESSION['PurchasingManagerEmail'] . "\r\n" . 'X-Mailer: PHP/' . phpversion();
54		if($_SESSION['SmtpSetting']==1){
55			include('includes/htmlMimeMail.php');
56			$mail = new htmlMimeMail();
57			$mail->setText($EmailText);
58			$mail->setSubject($Subject);
59			$mail->setFrom($_SESSION['PurchasingManagerEmail']);
60			$mail->setHeader('Reply-To',$_SESSION['PurchasingManagerEmail']);
61			$mail->setCc($_SESSION['PurchasingManagerEmail']); //Set this as a copy for filing purpose
62
63		}
64		foreach ($this->Suppliers as $Supplier) {
65		 if($_SESSION['SmtpSetting']==0){
66			 $result = mail($Supplier->EmailAddress, $Subject, $EmailText, $Headers);
67		 }else{
68			 $result = SendmailBySmtp($mail,array($Supplier->EmailAddress,$_SESSION['PurchasingManagerEmail']));
69		 }
70		}
71	}
72
73	function save() {
74		/* Does record exist for this tender
75		 */
76		if ($this->TenderId=='') {
77			$this->TenderId = GetNextTransNo(37);
78			$HeaderSQL="INSERT INTO tenders (tenderid,
79											location,
80											address1,
81											address2,
82											address3,
83											address4,
84											address5,
85											address6,
86											telephone,
87											requiredbydate)
88								VALUES ('" . $this->TenderId  . "',
89										'" . $this->Location  . "',
90										'" . $this->DelAdd1  . "',
91										'" . $this->DelAdd2  . "',
92										'" . $this->DelAdd3  . "',
93										'" . $this->DelAdd4  . "',
94										'" . $this->DelAdd5  . "',
95										'" . $this->DelAdd6  . "',
96										'" . $this->Telephone  . "',
97										'" . FormatDateForSQL($this->RequiredByDate) . "')";
98			foreach ($this->Suppliers as $Supplier) {
99				$SuppliersSQL[]="INSERT INTO tendersuppliers (tenderid,
100															supplierid,
101															email)
102								VALUES ('" . $this->TenderId . "',
103										'" . $Supplier->SupplierCode . "',
104										'" . $Supplier->EmailAddress . "')";
105			}
106			foreach ($this->LineItems as $LineItem) {
107				$ItemsSQL[]="INSERT INTO tenderitems (tenderid,
108														stockid,
109														quantity,
110														units)
111											VALUES ('" . $this->TenderId . "',
112													'" . $LineItem->StockID . "',
113													'" . $LineItem->Quantity . "',
114													'" . $LineItem->Units . "')";
115			}
116		} else {
117			$HeaderSQL="UPDATE tenders SET location='" . $this->Location  . "',
118											address1='" . $this->DelAdd1  . "',
119											address2='" . $this->DelAdd2  . "',
120											address3='" . $this->DelAdd3  . "',
121											address4='" . $this->DelAdd4  . "',
122											address5='" . $this->DelAdd5  . "',
123											address6='" . $this->DelAdd6  . "',
124											telephone='" . $this->Telephone  . "',
125											requiredbydate='" . FormatDateForSQL($this->RequiredByDate)  . "'
126						WHERE tenderid = '" . $this->TenderId  . "'";
127			foreach ($this->Suppliers as $Supplier) {
128				$sql="DELETE FROM tendersuppliers
129					WHERE  tenderid='" . $this->TenderId . "'";
130				$result=DB_query($sql);
131				$SuppliersSQL[]="INSERT INTO tendersuppliers (
132									tenderid,
133									supplierid,
134									email)
135								VALUES ('" . $this->TenderId . "',
136										'" . $Supplier->SupplierCode . "',
137										'" . $Supplier->EmailAddress . "')";
138			}
139			foreach ($this->LineItems as $LineItem) {
140				$sql="DELETE FROM tenderitems
141						WHERE  tenderid='" . $this->TenderId . "'";
142				$result=DB_query($sql);
143				$ItemsSQL[]="INSERT INTO tenderitems (tenderid,
144														stockid,
145														quantity,
146														units)
147								VALUES ('" . $this->TenderId . "',
148										'" . $LineItem->StockID . "',
149										'" . $LineItem->Quantity . "',
150										'" . $LineItem->Units . "')";
151			}
152		}
153		DB_Txn_Begin();
154		$result=DB_query($HeaderSQL, '', '', True);
155		foreach ($SuppliersSQL as $sql) {
156			$result=DB_query($sql, '', '', True);
157		}
158		foreach ($ItemsSQL as $sql) {
159			$result=DB_query($sql, '', '', True);
160		}
161		DB_Txn_Commit();
162	}
163
164	function add_item_to_tender(	$LineNo,
165								$StockID,
166								$Qty,
167								$ItemDescr,
168								$UOM,
169								$DecimalPlaces,
170								$ExpiryDate){
171
172		if (isset($Qty) and $Qty!=0){
173
174			$this->LineItems[$LineNo] = new LineDetails($LineNo,
175														$StockID,
176														$Qty,
177														$ItemDescr,
178														$UOM,
179														$DecimalPlaces,
180														$ExpiryDate);
181			$this->LinesOnTender++;
182			Return 1;
183		}
184		Return 0;
185	}
186
187	function add_supplier_to_tender(	$SupplierCode,
188									$SupplierName,
189									$Emailaddress){
190
191		if (!isset($this->Suppliers[$SupplierCode])){
192
193			$this->Suppliers[$SupplierCode] = new Supplier($SupplierCode, $SupplierName, $Emailaddress);
194			$this->SuppliersOnTender++;
195			Return 1;
196		}
197		Return 0;
198	}
199
200	function update_tender_item($LineNo,
201								$Qty,
202								$Price,
203								$ExpiryDate){
204
205			$this->LineItems[$LineNo]->Quantity = $Qty;
206			$this->LineItems[$LineNo]->Price = $Price;
207			$this->LineItems[$LineNo]->ExpiryDate = $ExpiryDate;
208	}
209
210	function remove_item_from_tender(&$LineNo){
211		unset($this->LineItems[$LineNo]);
212		$this->LinesOnTender--;
213	}
214
215	function remove_supplier_from_tender(&$SupplierCode){
216		unset($this->Suppliers[$SupplierCode]);
217		$this->SuppliersOnTender--;
218	}
219
220	function Tender_Value() {
221		$TotalValue=0;
222		foreach ($this->LineItems as $OrderedItems) {
223			$TotalValue += ($OrderedItems->Price)*($OrderedItems->Quantity);
224		}
225		return $TotalValue;
226	}
227} /* end of class defintion */
228
229Class LineDetails {
230/* PurchOrderDetails */
231	var $LineNo;
232	var $StockID;
233	var $ItemDescription;
234	var $Quantity;
235	var $Price;
236	var $Units;
237	var $DecimalPlaces;
238	var $Deleted;
239	var $ExpiryDate;
240
241	function __construct ($LineNo,
242							$StockItem,
243							$Qty,
244							$ItemDescr,
245							$UOM,
246							$DecimalPlaces,
247							$ExpiryDate) {
248
249	/* Constructor function to add a new LineDetail object with passed params */
250		$this->LineNo = $LineNo;
251		$this->StockID =$StockItem;
252		$this->ItemDescription = $ItemDescr;
253		$this->Quantity = $Qty;
254		$this->Units = $UOM;
255		$this->DecimalPlaces = $DecimalPlaces;
256		$this->ExpiryDate = $ExpiryDate;
257		$this->Deleted = False;
258	}
259
260	function LineDetails($LineNo,
261							$StockItem,
262							$Qty,
263							$ItemDescr,
264							$UOM,
265							$DecimalPlaces,
266							$ExpiryDate) {
267		self::__construct($LineNo,
268							$StockItem,
269							$Qty,
270							$ItemDescr,
271							$UOM,
272							$DecimalPlaces,
273							$ExpiryDate);
274	}
275
276}
277
278Class Supplier {
279
280	var $SupplierCode;
281	var $SupplierName;
282	var $EmailAddress;
283	var $Responded;
284
285	function __construct ($SupplierCode,
286						$SupplierName,
287						$EmailAddress) {
288		$this->SupplierCode = $SupplierCode;
289		$this->SupplierName = $SupplierName;
290		$this->EmailAddress = $EmailAddress;
291		$this->Responded = 0;
292	}
293
294	function Supplier ($SupplierCode,
295						$SupplierName,
296						$EmailAddress) {
297		self::__construct($SupplierCode,
298						$SupplierName,
299						$EmailAddress);
300	}
301
302
303}
304
305?>
306