<? include('includes/main.php'); ?>
<? include('includes/apfunctions.php'); ?>
<? include('includes/glfunctions.php'); ?>
<? include('includes/prfunctions.php'); ?>
<script language="JavaScript">
var calDateFormat='yyyy-MM-DD';
</script>
<script language="JavaScript" src="js/calendar.js"></script>
<? //invpotoap.php - pass received PO's to payables for payment.
echo texttitle('Inventory PO Pass to Payables');
if ($submit=='Show List') {
if (!$dateofinvoice||!$invoicenumber||!$vendorid) die(texterror("Must have a Vendor, Invoice Date AND Invoice Number"));
if ($vendorid) $vendorstr=' and invreceive.vendorid='.sqlprep($vendorid);
if ($locationid) $locationidstr=' and invreceive.locationid='.sqlprep($locationid);
echo '<form action="invpotoap.php" method="post" name="mainform">';
$recordSet = &$conn->Execute('select count(*) from invreceive,invpo,item where invreceive.passtoap=0 and item.id=invreceive.itemid and invreceive.gencompanyid='.sqlprep($active_company).$vendorstr.$locationidstr.' and invpo.id=invreceive.invpoid');
if ($recordSet->fields[0]>0) {
$recordSet1 = &$conn->Execute('select invreceive.itemid,invreceive.itemqty,invreceive.itemprice,invreceive.conversion,invreceive.receivedate,invreceive.invpoid,invpo.ponumber,item.itemcode,item.description,invreceive.id,item.inventoryglacctid from invreceive,invpo,item where invreceive.passtoap=0 and item.id=invreceive.itemid and invreceive.gencompanyid='.sqlprep($active_company).$vendorstr.$locationidstr.' and invpo.id=invreceive.invpoid order by item.itemcode, invreceive.receivedate');
echo '<table border="1"><tr><th>Include?</th><th>PO Number</th><th>Item</th><th>Received</th><th>Qty</th><th>Price per Unit</th></tr>';
$dcount=0;
while (!$recordSet1->EOF) {
$dcount+=1;
echo '<tr><td><input type="checkbox" name="check'.$dcount.'" value="1" onclick="POsum()"'.checkequal(${"check".$dcount},1,' checked').'></td>'; // onChange="addtotal(this)"></td>';
echo '<input type="hidden" name="invreceiveid'.$dcount.'" value="'.$recordSet1->fields[9].'">';
echo '<input type="hidden" name="glacctid'.$dcount.'" value="'.$recordSet1->fields[10].'">';
echo '<input type="hidden" name="conversion'.$dcount.'" value="'.$recordSet1->fields[3].'">';
echo '<input type="hidden" name="invpoid'.$dcount.'" value="'.$recordSet1->fields[5].'">';
echo '<td>'.$recordSet1->fields[6].'</td><td>'.$recordSet1->fields[7].' - '.$recordSet1->fields[8].'</td>';
echo '<td>'.$recordSet1->fields[4].'</td><td><input type="text" name="itemqty'.$dcount.'" onchange="validatenum(this)" value="'.checkdec($recordSet1->fields[1],0).'" onchange="POsum()" size="20" maxlength="20" '.INC_TEXTBOX.'></td>';
echo '<td><nobr><input type="text" name="price'.$dcount.'" onchange="validatenum(this)" value="'.checkdec($recordSet1->fields[2],PREFERRED_DECIMAL_PLACES).'" onchange="POsum()" size="20" maxlength="20" '.INC_TEXTBOX.'></nobr></td></tr>';
$recordSet1->MoveNext();
};
echo '<input type="hidden" name="invoicenumber" value="'.$invoicenumber.'">';
echo '<input type="hidden" name="dateofinvoice" value="'.$dateofinvoice.'">';
echo '<input type="hidden" name="description" value="'.$dateofinvoice.'">';
echo '<script language="JavaScript">'."\n";
echo ' function POsum() {'."\n";
echo ' numb = "0"'."\n";
for ($i=1; $i<=$dcount; $i++) echo ' numb = eval(numb) + round(eval(document.mainform.itemqty'.$i.'.value) * eval(document.mainform.price'.$i.'.value) * eval(document.mainform.check'.$i.'.checked),'.PREFERRED_DECIMAL_PLACES.")\n";
echo ' document.mainform.billtotal.value="'.CURRENCY_SYMBOL.'" + numb'."\n";
echo ' }'."\n";
echo ' function round(number,X) {'."\n";
echo ' X = (!X ? 0 : X);'."\n";
echo ' return Math.round(number*Math.pow(10,X))/Math.pow(10,X);'."\n";
echo ' }'."\n";
echo '</script>'."\n";
echo '<td>Total '.CURRENCY_SYMBOL.' selected:</td><td colspan="2"><input type="text" disabled name="billtotal" value="'.CURRENCY_SYMBOL.'0" onchange="POsum()" size="20" maxlength="20" '.INC_TEXTBOX.'></td></tr></table>';
echo '<input type="hidden" name="dcount" value="'.$dcount.'">';
echo '<input type="hidden" name="locationid" value="'.$locationid.'">';
echo '<input type="hidden" name="vendorid" value="'.$vendorid.'">';
echo '<input type="submit" name="submit" value="End Selections"></form>';
} else {
//none match
echo texterror('No Receipts found for this vendor.');
};
} else if ($submit=="End Selections") {
$billtotal=0;
for ($d=1; $d<=$dcount; $d++) {
if (${"check".$d}==1) {
$billtotal+=${"price".$d}*${"itemqty".$d};
};
};
// now ask for header bill info (invoice#, invoice date, freight
// additional, sales tax additional, etc.
echo '<form action="invpotoap.php" method="post" name="mainform"><table>';
$recordSet = &$conn->Execute('select vendor.defaultbilldescription, vendor.defaultglacctid, vendor.customeraccount, vendor.paytocompanyid, company.companyname, invoiceterms.discountpercent, invoiceterms.discountdayofmonth, invoiceterms.discountdays, invoiceterms.netduedays, invoiceterms.verbal, vendor.paytermsid from vendor,invoiceterms,company where vendor.id='.sqlprep($vendorid).' and vendor.paytocompanyid=company.id and vendor.paytermsid=invoiceterms.id');
if (!$recordSet->EOF) {
//read in terms info for calculating discount, net, etc.
$defaultbilldescription=$recordSet->fields[0];
$defaultglacctid=$recordSet->fields[1];
$customeraccount=$recordSet->fields[2];
$payvendorid=$recordSet->fields[3];
$vendorname=$recordSet->fields[4];
$discountpercent=$recordSet->fields[5];
$discountdayofmonth=$recordSet->fields[6];
$discountdays=$recordSet->fields[7];
$netduedays=$recordSet->fields[8];
$verbalterms=$recordSet->fields[9];
$vendorpaytermsid=$recordSet->fields[10];
$prepaid=0;
$discountamount=$billtotal*$discountpercent/100;
$discountdate=billdiscountdate($vendorpaytermsid,$dateofinvoice);
$duedate=billduedate($vendorpaytermsid,$dateofinvoice);
//display vendor name and terms at top of screen
echo '<tr><th colspan="2">Vendor: '.$vendorname.'</th></tr>';
echo '<tr><th colspan="2">Terms: '.$verbalterms.'</th></tr> ';
echo '<tr><th colspan="2">Invoice Date: '.$dateofinvoice.'</th></tr>';
echo '<tr><th colspan="2">Invoice Number: '.$invoicenumber.'</th></tr><tr><td></td></tr>';
echo '<input type="hidden" name="dateofinvoice" value="'.$dateofinvoice.'">';
echo '<input type="hidden" name="description" value="'.$description.'">';
echo '<input type="hidden" name="invoicenumber" value="'.$invoicenumber.'">';
echo '<input type="hidden" name="dcount" value="'.$dcount.'">';
echo '<input type="hidden" name="vendorid" value="'.$vendorid.'">';
for ($d=1; $d<=$dcount; $d++) {
echo '<input type="hidden" name="check'.$d.'" value="'.${"check".$d}.'">';
echo '<input type="hidden" name="price'.$d.'" value="'.${"price".$d}.'">';
echo '<input type="hidden" name="glacctid'.$d.'" value="'.${"glacctid".$d}.'">';
echo '<input type="hidden" name="itemqty'.$d.'" value="'.${"itemqty".$d}.'">';
echo '<input type="hidden" name="invreceiveid'.$d.'" value="'.${"invreceiveid".$d}.'">';
echo '<input type="hidden" name="conversion'.$d.'" value="'.${"conversion".$d}.'">';
echo '<input type="hidden" name="invpoid'.$d.'" value="'.${"invpoid".$d}.'">';
};
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'"><b>Total POs Received:</b></td><th>'.number_format($billtotal,2).'</th></tr>';
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Freight:</td><td><input type="text" name="freight" onchange="validatenum(this)" value="'.number_format($freight,2).'" size="20" maxlength="20" '.INC_TEXTBOX.'>';
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Tax:</td><td><input type="text" name="tax" onchange="validatenum(this)" value="'.number_format($tax,2).'" size="20" maxlength="20" '.INC_TEXTBOX.'> ';
echo '<tr></tr>';
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Discount Date:</td><td><input type="text" name="discountdate" onchange="formatDate(this)" value="'.$discountdate.'" size="20" maxlength="20" '.INC_TEXTBOX.'><a href="javascript:doNothing()" onclick="setDateField(document.mainform.discountdate); top.newWin = window.open(\'calendar.html\',\'cal\',\'dependent=yes,width=210,height=230,screenX=200,screenY=300,titlebar=yes\')"><img src="images/calendar.gif" border="0" alt="Display Calendar"></a></td></tr>';
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Due Date:</td><td><input type="text" name="duedate" onchange="formatDate(this)" value="'.$duedate.'" size="20" maxlength="20" '.INC_TEXTBOX.'><a href="javascript:doNothing()" onclick="setDateField(document.mainform.duedate); top.newWin = window.open(\'calendar.html\',\'cal\',\'dependent=yes,width=210,height=230,screenX=200,screenY=300,titlebar=yes\')"><img src="images/calendar.gif" border="0" alt="Display Calendar"></a></td></tr>';
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Discount Amount:</td><td><input type="text" name="discountamount" onchange="validatenum(this)" value="'.number_format($discountamount,2).'" size="20" maxlength="20" '.INC_TEXTBOX.'> ';
$a="unchecked" ;
if ($prepaid==1) $a="checked";
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Pre-Paid:</td><td><input type="checkbox" name="prepaid" value="'.$prepaid.'" '.$a.' </td></tr>';
echo '</table>';
echo '<input type="submit" name="submit" value="Save Invoice"></form>';
} else {
die (texterror("vendor data not found"));
};
} else if ($submit=="Save Invoice") {
//read general ap posting accounts first
$recordSet=&$conn->Execute('select payable,interestexpense,usetransactiondate from apcompany where id='.sqlprep($active_company));
if (!$recordSet->EOF) {
$payable=$recordSet->fields[0];
$interestexpense=$recordSet->fields[1];
$usetransactiondate=$recordSet->fields[2];
};
$recordSet=&$conn->Execute('select freight,tax from invcompany where id='.sqlprep($active_company));
if (!$recordSet->EOF) {
$glfreight=$recordSet->fields[0];
$gltax=$recordSet->fields[1];
};
$timestamp = time();
$date_time_array = getdate($timestamp);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
$today=date("Y-m-d", $timestamp);
$postdate=$dateofinvoice;
if (!$usetransactiondate) $postdate=$today;
// add up invoice totals
$billtotal=0;
for ($d=1; $d<=$dcount; $d++) {
if (${"check".$d}==1) {
$billtotal+=${"price".$d}*${"itemqty".$d};
};
};
$billtotal+=$tax+$freight;
// save to apbill
$wherefrom=1;
$apbillid=billadd($invoicenumber,$billtotal,$description,$dateofinvoice,$duedate,$discountamount,$discountdate,$vendorid,$wherefrom,$cancel);
if ($apbillid) {
//first add general information to gltransvoucher
$voucherid=gltransvoucheradd($invoicenumber,$description,$postdate,1);
if (!$voucherid) {
billdeleteadd($apbillid);
die(texterror('Error adding gltransvoucher record - BILL ENTRY CANCELLED'));
};
//write info to gltransaction file for payables side of post
$fail=gltransactionadd($voucherid, -$billtotal,$payable);
if (!$fail) {
//need to remove all gl transactions!
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
die(texterror('Error adding gl transaction details - BILL ENTRY CANCELLED'));
};
} else {
if (!$apbillid) die(texterror('Error creating Payables Invoice Entry, Entries Cancelled'));
};
// save to apbilldetail
for ($d=1; $d<=$dcount; $d++) {
if (${"check".$d}==1) { // only those items checked!
if (!billdetailadd($apbillid,${"price".$d}*${"itemqty".$d},${"glacctid".$d},${"invreceiveid".$d})) {
//need to remove all gl transactions!
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
billdetetebybillid($apbillid);
die(texterror('Error adding bill details - BILL ENTRY CANCELLED'));
} else {
//need to write info to gltransaction file for details
$fail=gltransactionadd($voucherid, ${"price".$d}*${"itemqty".$d},${"glacctid".$d});
if (!$fail) {
//need to remove all gl transactions!
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
billdeletebybillid($apbillid);
die(texterror('Error adding gl transaction details - BILL ENTRY CANCELLED'));
};
};
// update flag in invreceive file to show it has been billed and correct price, qty
if ($conn->Execute('update invreceive set itemqtyused='.sqlprep(${"itemqty".$d}).', itemprice='.sqlprep(${"price".$d}).', passtoap=1, apbillid='.sqlprep($apbillid).' where id='.sqlprep(${"invreceiveid".$d})) == false) {
echo texterror("Error receive record.");
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
billdeletebybillid($apbillid);
if ($conn->Execute('update invreceive set itemqtyused=0,apbillid=null where apbillid='.sqlprep($apbillid))==false) {
};
die(texterror('Error updating receive error - BILL ENTRY CANCELLED'));
};
};
//save freight to apbilldetail
if (!$freight==0) {
if (!billdetailadd($apbillid,$freight,$glfreight,0)) {
//need to remove all gl transactions!
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
billdetetebybillid($apbillid);
die(texterror('Error adding bill details - BILL ENTRY CANCELLED'));
} else {
//write info to gltransaction file for freight expense
$fail=gltransactionadd($voucherid, $freight,$glfreight);
if (!$fail) {
//need to remove all gl transactions!
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
die(texterror('Error adding gl transaction details - BILL ENTRY CANCELLED'));
};
};
};
//save tax to apbilldetail
if (!$tax==0) {
if (!billdetailadd($apbillid,$tax,$gltax,0)) {
//need to remove all gl transactions!
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
billdetetebybillid($apbillid);
die(texterror('Error adding bill details - BILL ENTRY CANCELLED'));
} else {
//write info to gltransaction file for tax expense
$fail=gltransactionadd($voucherid, $tax,$gltax);
if (!$fail) {
//need to remove all gl transactions!
gltransactiondelete($voucherid);
gltransvoucherdelete($voucherid);
billdeleteadd($apbillid);
die(texterror('Error adding gl transaction details - BILL ENTRY CANCELLED'));
};
};
};
};
// if paid flag set, then need to ask payment details AFTER save
} else {
echo '<form action="invpotoap.php" method="post" name="mainform"><table>';
$recordSet = &$conn->Execute('select count(*) from vendor, company where vendor.gencompanyid='.sqlprep($active_company).' and vendor.orderfromcompanyid=company.id and vendor.cancel=0');
if (!$recordSet->EOF) {
if ($recordSet->fields[0]>1) {
formapvendorselect('vendorid');
} elseif ($recordSet->fields[0]>0) {
$recordSet = &$conn->Execute('select vendor.id from vendor, company where vendor.gencompanyid='.sqlprep($active_company).' and vendor.orderfromcompanyid=company.id and vendor.cancel=0 order by company.companyname');
if (!$recordSet->EOF) {
$vendorid=$recordSet->fields[0];
$recordSet->MoveNext() ;
};
echo '<input type="hidden" name="vendorid" value="'.$vendorid.'">';
} else {
die (texterror('No Vendors in File'));
};
} else {
die (texterror('No Vendors in File'));
};
$recordSet = &$conn->Execute('select count(*) from inventorylocation,company where inventorylocation.companyid=company.id and inventorylocation.gencompanyid='.sqlprep($active_company));
if (!$recordSet->EOF) {
if ($recordSet->fields[0]>1) {
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Location:</td><td><select name="locationid"'.INC_TEXTBOX.'><option value="0">';
$recordSet = &$conn->Execute('select inventorylocation.id,company.companyname from inventorylocation,company where inventorylocation.companyid=company.id and inventorylocation.gencompanyid='.sqlprep($active_company).' order by company.companyname');
while (!$recordSet->EOF) {
echo '<option value="'.$recordSet->fields[0].'">'.$recordSet->fields[1]."\n";
$recordSet->MoveNext();
};
echo '</select></td></tr>';
} elseif ($recordSet->fields[0]>0) {
$recordSet = &$conn->Execute('select inventorylocation.id,company.companyname from inventorylocation,company where inventorylocation.companyid=company.id and inventorylocation.gencompanyid='.sqlprep($active_company).' order by company.companyname');
if (!$recordSet->EOF) {
$locationid=$recordSet->fields[0];
$recordSet->MoveNext() ;
};
echo '<input type="hidden" name="locationid" value="'.$locationid.'">';
} else {
die (texterror('No Locations in File'));
};
} else {
die (texterror("No Locations in File"));
};
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Invoice Number:</td><td><input type="text" name="invoicenumber" value="'.$invoicenumber.'" size="20" maxlength="20" '.INC_TEXTBOX.'> ';
if (!$dateofinvoice) $dateofinvoice=createtime('Y-m-d');
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">Invoice Date:</td><td><input type="text" name="dateofinvoice" value="'.$dateofinvoice.'" size="20" maxlength="20"'.INC_TEXTBOX.'><a href="javascript:doNothing()" onclick="setDateField(document.mainform.dateofinvoice); top.newWin = window.open(\'calendar.html\',\'cal\',\'dependent=yes,width=210,height=230,screenX=200,screenY=300,titlebar=yes\')"><img src="images/calendar.gif" border="0" alt="Display Calendar"></a></td></tr>';
echo '</table><input type="submit" name="submit" value="Show List"></form>';
};
?>
<? include('includes/footer.php'); ?>