Skip to content

Commit

Permalink
add option to import po items
Browse files Browse the repository at this point in the history
  • Loading branch information
expressachiever authored and express achiever committed Dec 6, 2018
1 parent 0615104 commit c24c521
Showing 1 changed file with 175 additions and 1 deletion.
176 changes: 175 additions & 1 deletion PO_Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,174 @@
$_SESSION['PO_ItemsResubmitForm' . $identifier]++; //change the $_SESSION VALUE
} /* end of if its a new item */

if (isset($_POST['UploadFile'])) {
if (isset($_FILES['CSVFile']) && $_FILES['CSVFile']['name']) {
//check file info
$FileName = $_FILES['CSVFile']['name'];
$TempName = $_FILES['CSVFile']['tmp_name'];
$FileSize = $_FILES['CSVFile']['size'];
//get file handle
$FileHandle = fopen($TempName, 'r');
$Row = 0;
$InsertNum = 0;

while (($FileRow = fgetcsv($FileHandle, 10000, ",")) !== False) {
++$Row;
if (filter_number_format($FileRow[1])!=0) { //if the form variable represents a Qty to add to the order

$ItemCode = $FileRow[0];
$Quantity = $FileRow[1];
$AlreadyOnThisOrder = 0;

if ($_SESSION['PO_AllowSameItemMultipleTimes'] ==false){
if (count($_SESSION['PO'.$identifier]->LineItems)!=0){

foreach ($_SESSION['PO'.$identifier]->LineItems AS $OrderItem) {

/* do a loop round the items on the order to see that the item is not already on this order */
if (($OrderItem->StockID == $ItemCode) AND ($OrderItem->Deleted==false)) {
$AlreadyOnThisOrder = 1;
prnMsg( _('The item') . ' ' . $ItemCode . ' ' . _('is already on this order') . '. ' . _('The system will not allow the same item on the order more than once') . '. ' . _('However you can change the quantity ordered of the existing line if necessary'),'error');
}
} /* end of the foreach loop to look for preexisting items of the same code */
}
}
if ($AlreadyOnThisOrder!=1 AND filter_number_format($Quantity) > 0){
$sql = "SELECT description,
longdescription,
stockid,
units,
decimalplaces,
stockact,
accountname
FROM stockmaster INNER JOIN stockcategory
ON stockcategory.categoryid = stockmaster.categoryid
INNER JOIN chartmaster
ON chartmaster.accountcode = stockcategory.stockact
WHERE stockmaster.stockid = '". $ItemCode . "'";

$ErrMsg = _('The item details for') . ' ' . $ItemCode . ' ' . _('could not be retrieved because');
$DbgMsg = _('The SQL used to retrieve the item details but failed was');
$ItemResult = DB_query($sql,$ErrMsg,$DbgMsg);
if (DB_num_rows($ItemResult)==1){
$ItemRow = DB_fetch_array($ItemResult);

$sql = "SELECT price,
conversionfactor,
supplierdescription,
suppliersuom,
suppliers_partno,
leadtime,
MAX(purchdata.effectivefrom) AS latesteffectivefrom
FROM purchdata
WHERE purchdata.supplierno = '" . $_SESSION['PO'.$identifier]->SupplierID . "'
AND purchdata.effectivefrom <='" . Date('Y-m-d') . "'
AND purchdata.stockid = '". $ItemCode . "'
GROUP BY purchdata.price,
purchdata.conversionfactor,
purchdata.supplierdescription,
purchdata.suppliersuom,
purchdata.suppliers_partno,
purchdata.leadtime
ORDER BY latesteffectivefrom DESC";

$ErrMsg = _('The purchasing data for') . ' ' . $ItemCode . ' ' . _('could not be retrieved because');
$DbgMsg = _('The SQL used to retrieve the purchasing data but failed was');
$PurchDataResult = DB_query($sql,$ErrMsg,$DbgMsg);
if (DB_num_rows($PurchDataResult)>0){ //the purchasing data is set up
$PurchRow = DB_fetch_array($PurchDataResult);

/* Now to get the applicable discounts */
$sql = "SELECT discountpercent,
discountamount
FROM supplierdiscounts
WHERE supplierno= '" . $_SESSION['PO'.$identifier]->SupplierID . "'
AND effectivefrom <='" . Date('Y-m-d') . "'
AND effectiveto >='" . Date('Y-m-d') . "'
AND stockid = '". $ItemCode . "'";

$ItemDiscountPercent = 0;
$ItemDiscountAmount = 0;
$ErrMsg = _('Could not retrieve the supplier discounts applicable to the item');
$DbgMsg = _('The SQL used to retrive the supplier discounts that failed was');
$DiscountResult = DB_query($sql,$ErrMsg,$DbgMsg);
while ($DiscountRow = DB_fetch_array($DiscountResult)) {
$ItemDiscountPercent += $DiscountRow['discountpercent'];
$ItemDiscountAmount += $DiscountRow['discountamount'];
}
if ($ItemDiscountPercent != 0) {
prnMsg(_('Taken accumulated supplier percentage discounts of') . ' ' . locale_number_format($ItemDiscountPercent*100,2) . '%','info');
}
if ($ItemDiscountAmount != 0 ){
prnMsg(_('Taken accumulated round sum supplier discount of') . ' ' . $_SESSION['PO'.$identifier]->CurrCode . ' ' . locale_number_format($ItemDiscountAmount,$_SESSION['PO'.$identifier]->CurrDecimalPlaces) . ' (' . _('per supplier unit') . ')','info');
}
$PurchPrice = ($PurchRow['price']*(1-$ItemDiscountPercent) - $ItemDiscountAmount)/$PurchRow['conversionfactor'];
$ConversionFactor = $PurchRow['conversionfactor'];
if (mb_strlen($PurchRow['supplierdescription'])>2){
$SupplierDescription = $PurchRow['supplierdescription'];
} else {
$SupplierDescription = $ItemRow['description'];
}
$SuppliersUnitOfMeasure = $PurchRow['suppliersuom'];
$SuppliersPartNo = $PurchRow['suppliers_partno'];
$LeadTime = $PurchRow['leadtime'];
/* Work out the delivery date based on today + lead time
* if > header DeliveryDate then set DeliveryDate to today + leadtime
*/
$DeliveryDate = DateAdd(Date($_SESSION['DefaultDateFormat']),'d',$LeadTime);
if (Date1GreaterThanDate2($_SESSION['PO'.$identifier]->DeliveryDate,$DeliveryDate)){
$DeliveryDate = $_SESSION['PO'.$identifier]->DeliveryDate;
}
} else { // no purchasing data setup
$PurchPrice = 0;
$ConversionFactor = 1;
$SupplierDescription = $ItemRow['description'];
$SuppliersUnitOfMeasure = $ItemRow['units'];
$SuppliersPartNo = '';
$LeadTime=1;
$DeliveryDate = $_SESSION['PO'.$identifier]->DeliveryDate;
}

$_SESSION['PO'.$identifier]->add_to_order ($_SESSION['PO'.$identifier]->LinesOnOrder+1,
$ItemCode,
0, /*Serialised */
0, /*Controlled */
filter_number_format($Quantity)*$ConversionFactor, /* Qty */
$SupplierDescription,
$PurchPrice,
$ItemRow['units'],
$ItemRow['stockact'],
$DeliveryDate,
0,
0,
0,
0,
0,
$ItemRow['accountname'],
$ItemRow['decimalplaces'],
$SuppliersUnitOfMeasure,
$ConversionFactor,
$LeadTime,
$SuppliersPartNo);
++$InsertNum;
} else { //no rows returned by the SQL to get the item
prnMsg (_('The item code') . ' ' . $ItemCode . ' ' . _('does not exist in the database and therefore cannot be added to the order'),'error');
if ($debug==1){
echo '<br />' . $sql;
}
}
} /* end of if not already on the order */
} /* end if the $_POST has NewQty in the variable name */
}
}
$_SESSION['PO_ItemsResubmitForm' . $identifier]++; //change the $_SESSION VALUE
prnMsg($InsertNum . ' ' . _('of') . ' ' . $Row . ' ' . _('rows have been added to the order'), 'info');
} /* end of if its items uploaded from csv */


/* This is where the order as selected should be displayed reflecting any deletions or insertions*/

echo '<form id="form1" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?identifier=' . $identifier . '" method="post">';
echo '<form id="form1" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?identifier=' . $identifier . '" method="post" enctype="multipart/form-data">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';

Expand Down Expand Up @@ -1162,6 +1327,15 @@
</tr>
<tr><td></td>
<td><b>' . _('OR') . ' </b><a target="_blank" href="'.$RootPath.'/Stocks.php">' . _('Insert New Item') . '</a></td></tr>
<tr>
<td colspan="10">
<div class="centre">
<h2>' . _('Or') . '</h2>
' . _('Upload items from csv file') . '<input type="file" name="CSVFile" />
<input type="submit" name="UploadFile" value="' . _('Upload File') . '" />
</div>
</td>
</tr>
</table>
<br />
Expand Down

0 comments on commit c24c521

Please sign in to comment.