3 /* This is where the details specific to the recurring order are entered and the template committed to the database once the Process button is hit */
5 include('includes/DefineCartClass.php');
7 /* Session started in header.inc for password checking the session will contain the details of the order from the Cart class object. The details of the order come from SelectOrderItems.php */
10 include('includes/session.inc');
11 $title = _('Recurring Orders');
12 include('includes/header.inc');
15 if ($_GET['NewRecurringOrder']=='Yes'){
16 $NewRecurringOrder ='Yes';
17 } elseif ($_POST['NewRecurringOrder']=='Yes'){
18 $NewRecurringOrder ='Yes';
20 $NewRecurringOrder ='No';
21 if (isset($_GET['ModifyRecurringSalesOrder'])){
23 $_POST['ExistingRecurrOrderNo'] = $_GET['ModifyRecurringSalesOrder'];
25 /*Need to read in the existing recurring order template */
27 $_SESSION['Items'] = new cart
;
29 /*read in all the guff from the selected order into the Items cart */
31 $OrderHeaderSQL = 'SELECT recurringsalesorders.debtorno,
33 recurringsalesorders.branchcode,
34 recurringsalesorders.customerref,
35 recurringsalesorders.comments,
36 recurringsalesorders.orddate,
37 recurringsalesorders.ordertype,
38 salestypes.sales_type,
39 recurringsalesorders.shipvia,
40 recurringsalesorders.deliverto,
41 recurringsalesorders.deladd1,
42 recurringsalesorders.deladd2,
43 recurringsalesorders.deladd3,
44 recurringsalesorders.deladd4,
45 recurringsalesorders.deladd5,
46 recurringsalesorders.deladd6,
47 recurringsalesorders.contactphone,
48 recurringsalesorders.contactemail,
49 recurringsalesorders.freightcost,
50 debtorsmaster.currcode,
51 recurringsalesorders.fromstkloc,
52 recurringsalesorders.frequency,
53 recurringsalesorders.stopdate,
54 recurringsalesorders.lastrecurrence,
55 recurringsalesorders.autoinvoice
56 FROM recurringsalesorders,
59 WHERE recurringsalesorders.ordertype=salestypes.typeabbrev
60 AND recurringsalesorders.debtorno = debtorsmaster.debtorno
61 AND recurringsalesorders.recurrorderno = ' . $_GET['ModifyRecurringSalesOrder'];
63 $ErrMsg = _('The order cannot be retrieved because');
64 $GetOrdHdrResult = DB_query($OrderHeaderSQL,$db,$ErrMsg);
66 if (DB_num_rows($GetOrdHdrResult)==1) {
68 $myrow = DB_fetch_array($GetOrdHdrResult);
70 $_SESSION['Items']->DebtorNo
= $myrow['debtorno'];
71 /*CustomerID defined in header.inc */
72 $_SESSION['Items']->Branch
= $myrow['branchcode'];
73 $_SESSION['Items']->CustomerName
= $myrow['name'];
74 $_SESSION['Items']->CustRef
= $myrow['customerref'];
75 $_SESSION['Items']->Comments
= $myrow['comments'];
77 $_SESSION['Items']->DefaultSalesType
=$myrow['ordertype'];
78 $_SESSION['Items']->SalesTypeName
=$myrow['sales_type'];
79 $_SESSION['Items']->DefaultCurrency
= $myrow['currcode'];
80 $_SESSION['Items']->ShipVia
= $myrow['shipvia'];
81 $BestShipper = $myrow['shipvia'];
82 $_SESSION['Items']->DeliverTo
= $myrow['deliverto'];
83 $_SESSION['Items']->DeliveryDate
= ConvertSQLDate($myrow['deliverydate']);
84 $_SESSION['Items']->BrAdd1
= $myrow['deladd1'];
85 $_SESSION['Items']->BrAdd2
= $myrow['deladd2'];
86 $_SESSION['Items']->BrAdd3
= $myrow['deladd3'];
87 $_SESSION['Items']->BrAdd4
= $myrow['deladd4'];
88 $_SESSION['Items']->BrAdd5
= $myrow['deladd5'];
89 $_SESSION['Items']->BrAdd6
= $myrow['deladd6'];
90 $_SESSION['Items']->PhoneNo
= $myrow['contactphone'];
91 $_SESSION['Items']->Email
= $myrow['contactemail'];
92 $_SESSION['Items']->Location
= $myrow['fromstkloc'];
93 $_SESSION['Items']->Quotation
= 0;
94 $FreightCost = $myrow['freightcost'];
95 $_SESSION['Items']->Orig_OrderDate
= $myrow['orddate'];
96 $_POST['StopDate'] = ConvertSQLDate($myrow['stopdate']);
97 $_POST['StartDate'] = ConvertSQLDate($myrow['lastrecurrence']);
98 $_POST['Frequency'] = $myrow['frequency'];
99 $_POST['AutoInvoice'] = $myrow['autoinvoice'];
101 /*need to look up customer name from debtors master then populate the line items array with the sales order details records */
102 $LineItemsSQL = "SELECT recurrsalesorderdetails.stkcode,
103 stockmaster.description,
107 recurrsalesorderdetails.unitprice,
108 recurrsalesorderdetails.quantity,
109 recurrsalesorderdetails.discountpercent,
110 recurrsalesorderdetails.narrative,
111 locstock.quantity as qohatloc,
113 stockmaster.discountcategory,
114 stockmaster.decimalplaces
115 FROM recurrsalesorderdetails INNER JOIN stockmaster
116 ON recurrsalesorderdetails.stkcode = stockmaster.stockid
117 INNER JOIN locstock ON locstock.stockid = stockmaster.stockid
118 WHERE locstock.loccode = '" . $myrow['fromstkloc'] . "'
119 AND recurrsalesorderdetails.recurrorderno =" . $_GET['ModifyRecurringSalesOrder'];
121 $ErrMsg = _('The line items of the order cannot be retrieved because');
122 $LineItemsResult = db_query($LineItemsSQL,$db,$ErrMsg);
123 if (db_num_rows($LineItemsResult)>0) {
125 while ($myrow=db_fetch_array($LineItemsResult)) {
126 $_SESSION['Items']->add_to_cart($myrow['stkcode'],
128 $myrow['description'],
130 $myrow['discountpercent'],
138 $myrow['discountcategory'],
141 $myrow['decimalplaces'],
142 $myrow['narrative']);
143 /*Just populating with existing order - no DBUpdates */
145 } /* line items from sales order details */
146 } //end of checks on returned data set
151 if ((!isset($_SESSION['Items']) OR $_SESSION['Items']->ItemsOrdered
== 0) AND $NewRecurringOrder=='Yes'){
152 prnMsg(_('A new recurring order can only be created if an order template has already been created from the normal order entry screen') . '. ' . _('To enter an order template select sales order entry from the orders tab of the main menu'),'error');
153 include('includes/footer.inc');
158 if (isset($_POST['DeleteRecurringOrder'])){
159 $sql = 'DELETE FROM recurrsalesorderdetails WHERE recurrorderno=' . $_POST['ExistingRecurrOrderNo'];
160 $ErrMsg = _('Could not delete recurring sales order lines for the recurring order template') . ' ' . $_POST['ExistingRecurrOrderNo'];
161 $result = DB_query($sql,$db,$ErrMsg);
163 $sql = 'DELETE FROM recurringsalesorders WHERE recurrorderno=' . $_POST['ExistingRecurrOrderNo'];
164 $ErrMsg = _('Could not delete the recurring sales order template number') . ' ' . $_POST['ExistingRecurrOrderNo'];
165 $result = DB_query($sql,$db,$ErrMsg);
167 prnMsg(_('Successfully deleted recurring sales order template number') . ' ' . $_POST['ExistingRecurrOrderNo'],'success');
169 echo "<P><A HREF='$rootpath/SelectRecurringSalesOrder.php?" . SID
. "'>". _('Select A Recurring Sales Order Template') .'</A>';
171 unset($_SESSION['Items']->LineItems
);
172 unset($_SESSION['Items']);
173 include('includes/footer.inc');
176 If (isset($_POST['Process'])) {
178 If (!Is_Date($_POST['StartDate'])){
180 prnMsg(_('The last recurrance or start date of this recurring order must be a valid date in the format') . ' ' . $_SESSION['DefaultDateFormat'],'error');
182 If (!Is_Date($_POST['StopDate'])){
184 prnMsg(_('The end date of this recurring order must be a valid date in the format') . ' ' . $_SESSION['DefaultDateFormat'],'error');
186 If (Date1GreaterThanDate2 ($_POST['StartDate'],$_POST['StopDate'])){
188 prnMsg(_('The end date of this recurring order must be after the start date'),'error');
190 if (isset($_POST['MakeRecurringOrder']) AND $_POST['Quotation']==1){
192 prnMsg( _('A recurring order cannot be made from a quotation'),'error');
195 if ($InputErrors == 0 ){ /*Error checks above all passed ok so lets go*/
197 if ($NewRecurringOrder=='Yes'){
199 /* finally write the recurring order header to the database and then the line details*/
201 $DelDate = FormatDateforSQL($_SESSION['Items']->DeliveryDate
);
203 $HeaderSQL = "INSERT INTO recurringsalesorders (
227 '" . $_SESSION['Items']->DebtorNo
. "',
228 '" . $_SESSION['Items']->Branch
. "',
229 '". DB_escape_string($_SESSION['Items']->CustRef
) ."',
230 '". DB_escape_string($_SESSION['Items']->Comments
) ."',
231 '" . Date("Y-m-d H:i") . "',
232 '" . $_SESSION['Items']->DefaultSalesType
. "',
233 '" . DB_escape_string($_SESSION['Items']->DeliverTo
) . "',
234 '" . DB_escape_string($_SESSION['Items']->BrAdd1
) . "',
235 '" . DB_escape_string($_SESSION['Items']->BrAdd2
) . "',
236 '" . DB_escape_string($_SESSION['Items']->BrAdd3
) . "',
237 '" . DB_escape_string($_SESSION['Items']->BrAdd4
) . "',
238 '" . DB_escape_string($_SESSION['Items']->BrAdd5
) . "',
239 '" . DB_escape_string($_SESSION['Items']->BrAdd6
) . "',
240 '" . DB_escape_string($_SESSION['Items']->PhoneNo
) . "',
241 '" . DB_escape_string($_SESSION['Items']->Email
) . "',
242 " . $_SESSION['Items']->FreightCost
.",
243 '" . $_SESSION['Items']->Location
."',
244 '" . $_SESSION['Items']->ShipVia
."',
245 '" . FormatDateforSQL($_POST['StartDate']) . "',
246 '" . FormatDateforSQL($_POST['StopDate']) . "',
247 " . $_POST['Frequency'] .',
248 ' . $_POST['AutoInvoice'] . ')';
250 $ErrMsg = _('The recurring order cannot be added because');
251 $InsertQryResult = DB_query($HeaderSQL,$db,$ErrMsg);
253 $RecurrOrderNo = DB_Last_Insert_ID($db,'recurringsalesorders','recurrorderno');
254 $StartOf_LineItemsSQL = "INSERT INTO recurrsalesorderdetails (
263 foreach ($_SESSION['Items']->LineItems
as $StockItem) {
265 $LineItemsSQL = $StartOf_LineItemsSQL .
267 '" . $StockItem->StockID
. "',
268 ". $StockItem->Price
. ",
269 " . $StockItem->Quantity
. ",
270 " . $StockItem->DiscountPercent
. ",
271 '" . DB_escape_string($StockItem->Narrative
) . "'
273 $Ins_LineItemResult = DB_query($LineItemsSQL,$db);
274 } /* inserted line items into sales order details */
276 prnmsg(_('The new recurring order template has been added'),'success');
278 } else { /* must be updating an existing recurring order */
279 $HeaderSQL = "UPDATE recurringsalesorders SET
280 stopdate = '" . FormatDateforSQL($_POST['StopDate']) . "',
281 frequency = " . $_POST['Frequency'] . ",
282 autoinvoice = " . $_POST['AutoInvoice'] . '
283 WHERE recurrorderno = ' . $_POST['ExistingRecurrOrderNo'];
285 $ErrMsg = _('The recurring order cannot be updated because');
286 $UpdateQryResult = DB_query($HeaderSQL,$db,$ErrMsg);
287 prnmsg(_('The recurring order template has been updated'),'success');
290 echo "<P><A HREF='$rootpath/SelectOrderItems.php?" . SID
. "&NewOrder=Yes'>". _('Enter New Sales Order') .'</A>';
292 echo "<P><A HREF='$rootpath/SelectRecurringSalesOrder.php?" . SID
. "'>". _('Select A Recurring Sales Order Template') .'</A>';
294 unset($_SESSION['Items']->LineItems
);
295 unset($_SESSION['Items']);
296 include('includes/footer.inc');
302 echo '<CENTER><FONT SIZE=4><B>'. _('Recurring Order for Customer') .' : ' . $_SESSION['Items']->CustomerName
. '</B></FONT></CENTER>';
303 echo "<FORM ACTION='" . $_SERVER['PHP_SELF'] . '?' . $SID . "' METHOD=POST>";
307 <TABLE CELLPADDING=2 COLSPAN=7 BORDER=1>
309 <TD class='tableheader'>". _('Item Code') ."</TD>
310 <TD class='tableheader'>". _('Item Description') ."</TD>
311 <TD class='tableheader'>". _('Quantity') ."</TD>
312 <TD class='tableheader'>". _('Unit') ."</TD>
313 <TD class='tableheader'>". _('Price') ."</TD>
314 <TD class='tableheader'>". _('Discount') ." %</TD>
315 <TD class='tableheader'>". _('Total') ."</TD>
318 $_SESSION['Items']->total
= 0;
319 $_SESSION['Items']->totalVolume
= 0;
320 $_SESSION['Items']->totalWeight
= 0;
321 $k = 0; //row colour counter
323 foreach ($_SESSION['Items']->LineItems
as $StockItem) {
325 $LineTotal = $StockItem->Quantity
* $StockItem->Price
* (1 - $StockItem->DiscountPercent
);
326 $DisplayLineTotal = number_format($LineTotal,2);
327 $DisplayPrice = number_format($StockItem->Price
,2);
328 $DisplayQuantity = number_format($StockItem->Quantity
,$StockItem->DecimalPlaces
);
329 $DisplayDiscount = number_format(($StockItem->DiscountPercent
* 100),2);
333 echo "<tr bgcolor='#CCCCCC'>";
336 echo "<tr bgcolor='#EEEEEE'>";
340 echo "<TD>$StockItem->StockID</TD>
341 <TD>$StockItem->ItemDescription</TD>
342 <TD ALIGN=RIGHT>$DisplayQuantity</TD>
343 <TD>$StockItem->Units</TD>
344 <TD ALIGN=RIGHT>$DisplayPrice</TD>
345 <TD ALIGN=RIGHT>$DisplayDiscount</TD>
346 <TD ALIGN=RIGHT>$DisplayLineTotal</TD>
349 $_SESSION['Items']->total
= $_SESSION['Items']->total +
$LineTotal;
350 $_SESSION['Items']->totalVolume
= $_SESSION['Items']->totalVolume +
($StockItem->Quantity
* $StockItem->Volume
);
351 $_SESSION['Items']->totalWeight
= $_SESSION['Items']->totalWeight +
($StockItem->Quantity
* $StockItem->Weight
);
354 $DisplayTotal = number_format($_SESSION['Items']->total
,2);
356 <TD COLSPAN=6 ALIGN=RIGHT><B>". _('TOTAL Excl Tax/Freight') ."</B></TD>
357 <TD ALIGN=RIGHT>$DisplayTotal</TD>
361 <TD>'. _('Deliver To') .":</TD>
362 <TD>" . $_SESSION['Items']->DeliverTo
. "</TD></TR>";
365 <TD>'. _('Deliver from the warehouse at') .":</TD>
366 <TD>" . $_SESSION['Items']->Location
. '</TD></TR>';
369 <TD>'. _('Street') .":</TD>
370 <TD>" . $_SESSION['Items']->BrAdd1
. "</TD></TR>";
373 <TD>". _('Suburb') .":</TD>
374 <TD>" . $_SESSION['Items']->BrAdd2
. "</TD></TR>";
377 <TD>'. _('City') . '/' . _('Region') .':</TD>
378 <TD>' . $_SESSION['Items']->BrAdd3
. '</TD></TR>';
381 <TD>'. _('Post Code') .':</TD>
382 <TD>' . $_SESSION['Items']->BrAdd4
. '</TD></TR>';
385 <TD>'. _('Contact Phone Number') .':</TD>
386 <TD>' . $_SESSION['Items']->PhoneNo
. '</TD></TR>';
388 echo '<TR><TD>' . _('Contact Email') .':</TD>
389 <TD>' . $_SESSION['Items']->Email
. '</TD></TR>';
391 echo '<TR><TD>'. _('Customer Reference') .':</TD>
392 <TD>' . $_SESSION['Items']->CustRef
. '</TD></TR>';
395 <TD>'. _('Comments') .':</TD>
396 <TD>' . $_SESSION['Items']->Comments
.'</TD></TR>';
398 if (!isset($_POST['StartDate'])){
399 $_POST['StartDate'] = date($_SESSION['DefaultDateFormat']);
402 if ($NewRecurringOrder=='Yes'){
404 <TD>'. _('Start Date') .':</TD>
405 <TD><INPUT TYPE=TEXT NAME="StartDate" SIZE=11 MAXLENGTH=10 VALUE="' . $_POST['StartDate'] .'"</TD></TR>';
408 <TD>'. _('Last Recurrence') . ':</TD>
409 <TD>' . $_POST['StartDate'] . '</TD></TR>';
410 echo '<INPUT TYPE=HIDDEN NAME="StartDate" VALUE="' . $_POST['StartDate'] . '">';
413 if (!isset($_POST['StopDate'])){
414 $_POST['StopDate'] = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m'),Date('d')+
1,Date('y')+
1));
418 <TD>'. _('Finish Date') .':</TD>
419 <TD><INPUT TYPE=TEXT NAME="StopDate" SIZE=11 MAXLENGTH=10 VALUE="' . $_POST['StopDate'] .'"</TD></TR>';
422 <TD>'. _('Frequency of Recurrence') .':</TD>
423 <TD><SELECT NAME="Frequency">';
425 if ($_POST['Frequency']==52){
426 echo '<OPTION SELECTED VALUE=52>' . _('Weekly');
428 echo '<OPTION VALUE=52>' . _('Weekly');
430 if ($_POST['Frequency']==26){
431 echo '<OPTION SELECTED VALUE=26>' . _('Fortnightly');
433 echo '<OPTION VALUE=26>' . _('Fortnightly');
435 if ($_POST['Frequency']==12){
436 echo '<OPTION SELECTED VALUE=12>' . _('Monthly');
438 echo '<OPTION VALUE=12>' . _('Monthly');
440 if ($_POST['Frequency']==6){
441 echo '<OPTION SELECTED VALUE=6>' . _('Bi-monthly');
443 echo '<OPTION VALUE=6>' . _('Bi-monthly');
445 if ($_POST['Frequency']==4){
446 echo '<OPTION SELECTED VALUE=4>' . _('Quarterly');
448 echo '<OPTION VALUE=4>' . _('Quarterly');
450 if ($_POST['Frequency']==2){
451 echo '<OPTION SELECTED VALUE=2>' . _('Bi-Annually');
453 echo '<OPTION VALUE=2>' . _('Bi-Annually');
455 if ($_POST['Frequency']==1){
456 echo '<OPTION SELECTED VALUE=1>' . _('Annually');
458 echo '<OPTION VALUE=1>' . _('Annually');
460 echo '</SELECT></TD></TR>';
463 if ($_SESSION['Items']->AllDummyLineItems()==true){
465 echo '<TR><TD>' . _('Invoice Automatically') . ':</TD>
466 <TD><SELECT NAME="AutoInvoice">';
467 if ($_POST['AutoInvoice']==0){
468 echo '<OPTION SELECTED VALUE=0>' . _('No');
469 echo '<OPTION VALUE=1>' . _('Yes');
471 echo '<OPTION VALUE=0>' . _('No');
472 echo '<OPTION SELECTED VALUE=1>' . _('Yes');
474 echo '</SELECT></TD></TR>';
476 echo '<INPUT TYPE=HIDDEN NAME="AutoInvoice" VALUE=0>';
482 if ($NewRecurringOrder=='Yes'){
483 echo '<INPUT TYPE=HIDDEN NAME="NewRecurringOrder" VALUE="Yes">';
484 echo "<INPUT TYPE=SUBMIT NAME='Process' VALUE='" . _('Create Reccurring Order') . "'>";
486 echo '<INPUT TYPE=HIDDEN NAME="NewRecurringOrder" VALUE="No">';
487 echo '<INPUT TYPE=HIDDEN NAME="ExistingRecurrOrderNo" VALUE=' . $_POST['ExistingRecurrOrderNo'] . '>';
489 echo "<INPUT TYPE=SUBMIT NAME='Process' VALUE='" . _('Update Reccurring Order Details') . "'>";
491 echo '<BR><BR><INPUT TYPE=SUBMIT NAME="DeleteRecurringOrder" VALUE="' . _('Delete Recurring Order') . ' ' . $_POST['ExistingRecurrOrderNo'] . '" onclick="return confirm(\'' . _('Are you sure you wish to delete this recurring order template?') . '\');">';
494 echo '</FORM></CENTER>';
495 include('includes/footer.inc');