function write($_POST) { # get vars extract($_POST); $rounding += 0; $pcredit += 0; $vatamount = 0; # validate input require_lib("validate"); $v = new validate(); $v->isOk($invid, "num", 1, 20, "Invalid invoice number."); $v->isOk($prd, "num", 1, 20, "Invalid period number."); $v->isOk($comm, "string", 0, 255, "Invalid Comments."); $v->isOk($terms, "num", 1, 20, "Invalid terms."); $v->isOk($odate, "date", 1, 14, "Invalid Invoice note date."); $v->isOk($traddisc, "float", 0, 20, "Invalid Trade Discount."); $v->isOk($delchrg, "float", 0, 20, "Invalid Delivery Charge."); $v->isOk($SUBTOT, "float", 0, 20, "Invalid Delivery Charge."); # Used to generate errors $error = "asa@"; # Check quantities if (isset($qtys)) { foreach ($qtys as $keys => $qty) { $v->isOk($qty, "float", 1, 15, "Invalid Returned Quantity."); $v->isOk($disc[$keys], "float", 0, 20, "Invalid Discount."); $v->isOk($discp[$keys], "float", 0, 20, "Invalid Discount Percentage."); } } else { $v->isOk($error, "num", 0, 1, "Invalid Returned Quantity."); } # Check stkids[] if (isset($stkids)) { foreach ($stkids as $keys => $stkid) { $v->isOk($stkid, "num", 1, 10, "Invalid Stock number, please enter all details.{$stkid}"); } } else { $v->isOk($error, "num", 0, 1, "Invalid Stock number, please enter all details."); } # Check amt[] if (isset($amt)) { foreach ($amt as $keys => $amount) { $v->isOk($amount, "float", 1, 20, "Invalid Amount, please enter all details."); } } else { $v->isOk($error, "num", 0, 1, "Invalid Amount, please enter all details."); } # display errors, if any $err = ""; if ($v->isError()) { $errors = $v->getErrors(); foreach ($errors as $e) { $err .= "<li class='err'>" . $e["msg"] . "</li>"; } return error($_POST, $err); } pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF); /* -------------------------------- */ # Get invoice info db_conn($prd); $sql = "SELECT * FROM pinvoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $invRslt = db_exec($sql) or errDie("Unable to get invoice information"); if (pg_numrows($invRslt) < 1) { return "<i class='err'>Not Found</i>"; } $inv = pg_fetch_array($invRslt); if ($rounding > 0) { db_conn('core'); $Sl = "SELECT * FROM salesacc WHERE name='rounding'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set the rounding account, under sales settings."; } $ad = pg_fetch_array($Ri); $rac = $ad['accnum']; } $notenum = divlastid('note', USER_DIV); /* --- Start Products Display --- */ # Products layout $products = ""; $taxex = 0; foreach ($qtys as $keys => $value) { db_connect(); # get selamt from selected stock $sql = "SELECT * FROM stock WHERE stkid = '{$stkids[$keys]}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); # get selected stock in this invoice db_conn($prd); $sql = "SELECT * FROM pinv_items WHERE id = '{$sids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $stkd = pg_fetch_array($stkdRslt); #check serial number if (strlen($stkd['ss']) > 0) { $me = $stkd['ss']; } else { $me = $stkd['serno']; } #determine which table to connect to switch (substr($me, strlen($me) - 1, 1)) { case "0": $tab = "ss0"; break; case "1": $tab = "ss1"; break; case "2": $tab = "ss2"; break; case "3": $tab = "ss3"; break; case "4": $tab = "ss4"; break; case "5": $tab = "ss5"; break; case "6": $tab = "ss6"; break; case "7": $tab = "ss7"; break; case "8": $tab = "ss8"; break; case "9": $tab = "ss9"; break; default: $tab = "ss0"; } db_connect(); $upd = "UPDATE {$tab} SET active = 'yes' WHERE code = '{$stkd['ss']}' OR code = '{$stkd['serno']}'"; $run_upd = db_exec($upd) or errDie("Unable to update stock serial numbers"); # get warehouse name db_conn("exten"); $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); # Calculate the Discount discount if ($disc[$keys] < 1) { if ($discp[$keys] > 0) { $disc[$keys] = $discp[$keys] / 100 * $stkd['unitcost']; } } else { $discp[$keys] = $disc[$keys] * 100 / $stkd['unitcost']; } # Calculate amount $amt[$keys] = $qtys[$keys] * ($stkd['unitcost'] - $disc[$keys]); db_connect(); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $vatamount += $ivat; # Check Tax Excempt if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $taxex += $amt[$keys]; } if ($stkd['account'] != 0) { # put in product $products .= "\n\t\t\t<input type='hidden' name='vatcode[]' value='{$stkd['vatcode']}' />\n\t\t\t<tr>\n\t\t\t\t<td colspan='2'><input type='hidden' name='stkids[]' value='{$stk['stkid']}'>{$stkd['description']}</td>\n\t\t\t\t<td><input type='hidden' size='5' name='qtys[]' value='{$qtys[$keys]}'>{$qtys[$keys]}</td>\n\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$keys]}'>" . CUR . " {$amt[$keys]}</td>\n\t\t\t</tr>"; } else { # put in product $products .= "\n\t\t\t<input type='hidden' name='vatcode[]' value='{$stkd['vatcode']}' />\n\t\t\t<tr>\n\t\t\t\t<td><input type='hidden' name='stkids[]' value='{$stk['stkid']}'>{$stk['stkcod']}</td>\n\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t<td><input type='hidden' size='5' name='qtys[]' value='{$qtys[$keys]}'>{$qtys[$keys]}</td>\n\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t<td nowrap><input type='hidden' name='amt[]' value='{$amt[$keys]}'>" . CUR . " {$amt[$keys]}</td>\n\t\t\t</tr>"; } } # get department db_conn("exten"); $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'"; $deptRslt = db_exec($sql); if (pg_numrows($deptRslt) < 1) { $dept['deptname'] = "<i class='err'>Not Found</i>"; } else { $dept = pg_fetch_array($deptRslt); } /* calculate delivery charge vat */ db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$inv['delvat']}'"; $Ri = db_exec($Sl); $vd = pg_fetch_array($Ri); $vr = vatcalc($delchrg, $inv['chrgvat'], $vd['zero'] == "Yes" ? "y" : "", $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $vatamount += $ivat; /* --- ----------- Clac --------------------- # calculate subtot $SUBTOT = 0.00; if(isset($amt)) $SUBTOT = array_sum($amt); $SUBTOT -= $taxex; # duplicate $SUBTOTAL = $SUBTOT; $VATP = TAX_VAT; if($inv['chrgvat'] == "exc"){ $SUBTOTAL = $SUBTOTAL; $delexvat= ($delchrg); }elseif($inv['chrgvat'] == "inc"){ $SUBTOTAL = sprint(($SUBTOTAL * 100)/(100 + $VATP)); $delexvat = sprint(($delchrg * 100)/($VATP + 100)); }else{ $SUBTOTAL = ($SUBTOTAL); $delexvat = ($delchrg); } $SUBTOT = $SUBTOTAL; $EXVATTOT = $SUBTOT; $EXVATTOT += $delexvat; # Minus trade discount from taxex if($traddisc > 0){ $traddiscmtt = (($traddisc/100) * $taxex); }else{ $traddiscmtt = 0; } $taxext = ($taxex - $traddiscmtt); if($traddisc > 0) { $traddiscmt = ($EXVATTOT * ($traddisc/100)); }else{ $traddiscmt = 0; } $EXVATTOT -= $traddiscmt; // $EXVATTOT -= $taxex; $traddiscmt = sprint($traddiscmt + $traddiscmtt); $traddiscm = $traddiscmt; if($inv['chrgvat'] != "nov"){ $VAT = sprint($EXVATTOT * ($VATP/100)); }else{ $VAT = 0; } $TOTAL = sprint($EXVATTOT + $VAT + $taxext); $SUBTOT += $taxex; /* --- ----------- Clac --------------------- */ /* --- ----------- Clac --------------------- */ ##----------------------NEW---------------------- $chrgvat = $inv['chrgvat']; $sub = 0.0; if (isset($amt)) { $sub = sprint(array_sum($amt)); } $VATP = TAX_VAT; if ($chrgvat == "exc") { $taxex = sprint($taxex - $taxex * $traddisc / 100); $subtotal = sprint($sub + $delchrg); $traddiscmt = sprint($subtotal * $traddisc / 100); $subtotal = sprint($subtotal - $traddiscmt); //$VAT=sprint(($subtotal-$taxex)*$VATP/100); $VAT = sprint($vatamount); $SUBTOT = $sub; $TOTAL = sprint($subtotal + $VAT); $delexvat = sprint($delchrg); } elseif ($chrgvat == "inc") { $ot = $taxex; $taxex = sprint($taxex - $taxex * $traddisc / 100); $subtotal = sprint($sub + $delchrg); $traddiscmt = sprint($subtotal * $traddisc / 100); $subtotal = sprint($subtotal - $traddiscmt); //$VAT=sprint(($subtotal-$taxex)*$VATP/(100+$VATP)); $VAT = sprint($vatamount); $SUBTOT = sprint($sub); $TOTAL = sprint($subtotal); $delexvat = sprint($delchrg); $traddiscmt = sprint($traddiscmt); } else { $subtotal = sprint($sub + $delchrg); $traddiscmt = sprint($subtotal * $traddisc / 100); $subtotal = sprint($subtotal - $traddiscmt); $VAT = sprint(0); $SUBTOT = $sub; $TOTAL = $subtotal; $delexvat = sprint($delchrg); } /* --- ----------- Clac --------------------- */ ##----------------------END---------------------- # Get invoice info db_conn($prd); $sql = "SELECT * FROM pinvoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $invRslt = db_exec($sql) or errDie("Unable to get invoice information"); if (pg_numrows($invRslt) < 1) { return "<li class='err'>Invoice Not Found</li>"; } $inv = pg_fetch_array($invRslt); if ($inv['balance'] >= $TOTAL) { $invpay = $TOTAL; $examt = 0; } else { $invpay = $inv['balance']; $examt = $TOTAL - $invpay; } /* - Start Hooks - */ $vatacc = gethook("accnum", "salesacc", "name", "VAT", "VAT"); /* - End Hooks - */ # todays date $date = date("d-m-Y"); $sdate = date("Y-m-d"); $refnum = getrefnum(); /*refnum*/ # insert invoice to period DB if ($inv['cusnum'] != "0") { #then get the actual customer db_connect(); $get_cus = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' LIMIT 1"; $run_cus = db_exec($get_cus) or errDie("Unable to get customer information"); if (pg_numrows($run_cus) < 1) { #do nothing } else { $carr = pg_fetch_array($run_cus); $inv['cusname'] = "{$carr['cusname']}"; $inv['surname'] = "{$carr['surname']}"; } } db_conn($prd); # Format date $odate = explode("-", $odate); $rodate = $odate[2] . "-" . $odate[1] . "-" . $odate[0]; $td = $rodate; # Insert invoice credit note to DB $sql = "INSERT INTO inv_notes(deptid, notenum, invnum, invid, cusnum, cordno, ordno,\n\t\t\t\tchrgvat, terms, traddisc, salespn, odate, delchrg, subtot, vat, total, comm,\n\t\t\t\tusername, div, surname, cusaddr, cusvatno, telno, deptname, prd)"; $sql .= " VALUES('{$inv['deptid']}', '{$notenum}', '{$inv['invnum']}', '{$inv['invid']}',\n\t\t\t\t'{$inv['cusnum']}', '{$inv['cordno']}', '{$inv['ordno']}', '{$inv['chrgvat']}',\n\t\t\t\t'{$terms}', '{$traddiscmt}', '{$inv['salespn']}', '{$rodate}', '{$delexvat}',\n\t\t\t\t'{$SUBTOT}', '{$VAT}' , '{$TOTAL}', '{$comm}', '" . USER_NAME . "', '" . USER_DIV . "',\n\t\t\t\t'{$inv['cusname']} {$inv['surname']}', '{$inv['cusaddr']}', '{$inv['cusvatno']}', '{$inv['telno']}',\n\t\t\t\t'{$inv['deptname']}', {$inv['prd']})"; $rslt = db_exec($sql) or errDie("Unable to insert invoice to Cubit.", SELF); $invnum = $inv['invnum']; # Get next ordnum $noteid = pglib_lastid("inv_notes", "noteid"); db_conn($prd); # Begin updating $nbal = $inv['nbal'] + $TOTAL; # Update the invoice (make balance less) $sql = "UPDATE pinvoices SET nbal = '{$nbal}', rdelchrg = (rdelchrg + '{$delchrg}'), balance = balance - '{$invpay}' WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); $av = $VAT; $at = $TOTAL - $VAT; /* $inv['pcash']=$pcash; $inv['pcheque']=$pcheque; $inv['pcc']=$pcc; $inv['pcredit']=$pcredit;*/ $sd = date("Y-m-d"); db_conn('cubit'); $Sl = "SELECT * FROM payrec WHERE inv='{$invnum}'"; $Ri = db_exec($Sl); $data = pg_fetch_array($Ri); $user = $data['by']; $ro = $rounding; $ro += 0; $nsp = 0; # Commit updating $inv['pcash'] = $pcash; $inv['pcheque'] = $pcheque; $inv['pcc'] = $pcc; $inv['pcredit'] = $pcredit; $pcreditback = $pcredit; # Make ledge record //custledger($inv['cusnum'], $dept['incacc'], $td, $notenum, "Credit Note No. $notenum for invoice No. $inv[invnum]", $TOTAL, "c"); $commision = 0; if ($examt > 0) { # Make record for age analisys //custCTP($examt, $inv['cusnum'],$td); } $discs = 0; $salesp = qrySalesPersonN($inv["salespn"]); foreach ($qtys as $keys => $value) { db_connect(); # get selamt from selected stock $sql = "SELECT * FROM stock WHERE stkid = '{$stkids[$keys]}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); # get selected stock in this invoice db_conn($prd); $sql = "SELECT * FROM pinv_items WHERE id = '{$sids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; //print $sql; $stkdRslt = db_exec($sql); $stkd = pg_fetch_array($stkdRslt); $stkd['account'] += 0; if ($stkd['account'] == 0) { # Keep track of discounts $discs += $stkd['disc'] * $stkd['qty']; db_connect(); $Sl = "SELECT * FROM scr WHERE inv='{$inv['invnum']}' AND stkid='{$stkd['stkid']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) > 0) { $cd = pg_fetch_array($Ri); $stk['csprice'] = $cd['amount']; } else { $stk["csprice"] = 0; } # cost amount if ($stk['csprice'] == "0.00") { $cosamt = round($qtys[$keys] * $stk['lcsprice'], 2); } else { $cosamt = round($qtys[$keys] * $stk['csprice'], 2); } db_connect(); # Update stock(onhand + qty) $sql = "UPDATE stock SET csamt = (csamt + '{$cosamt}'), units = (units + '{$qtys[$keys]}') WHERE stkid = '{$stkids[$keys]}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update stock to Cubit.", SELF); # fix stock cost amount $Sl = "UPDATE stock set csprice=csamt/units WHERE stkid = '{$stkids[$keys]}' AND units>0"; $Ri = db_exec($Sl) or errDie("Unable to update stock cost price in Cubit.", SELF); if ($stk['serd'] == 'yes') { ext_InSer($stkd['serno'], $stkd['stkid'], "{$inv['cusname']} {$inv['surname']}", $notenum, 'note', $td); } # negetive values to minus profit $nqty = $qtys[$keys] * 1; $namt = $amt[$keys] * -1; $ncsprice = $cosamt * -1; $noted = $stkd['noted'] + $qtys[$keys]; # stkid, stkcod, stkdes, trantype, edate, qty, csamt, details stockrec($stkd['stkid'], $stk['stkcod'], $stk['stkdes'], 'dt', $td, $nqty, $cosamt, "Credit note for Customer : {$inv['surname']} - Credit note No. {$notenum}"); db_connect(); # Get amount exluding vat if including and not exempted $VATP = TAX_VAT; $amtexvat = $amt[$keys]; ###################VAT CALCS####################### $Sl = "SELECT * FROM cubit.vatcodes WHERE id='{$stk['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd["vat_amount"]); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $td, "OUTPUT", $vd['code'], $refnum, "VAT for Credit note: {$notenum} Customer : {$inv['surname']}", -$iamount, -$ivat); #################################################### $sql = "INSERT INTO stockrec(edate, stkid, stkcod, stkdes, trantype, qty, csprice, csamt, details, div)\n\t\t\t\tVALUES('{$td}', '{$stk['stkid']}', '{$stk['stkcod']}', '{$stk['stkdes']}', 'note', '{$qtys[$keys]}', '{$amtexvat}', '{$cosamt}', 'Credit note for Customer : {$inv['surname']} - Credit note No. {$notenum}', '" . USER_DIV . "')"; $recRslt = db_exec($sql); if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = $stk["com"]; } $commision = $commision + coms($inv['salespn'], $amt[$keys], $itemcommission); # Get selected stock in this invoice db_conn($prd); $sql = "UPDATE pinv_items SET noted = '{$noted}' WHERE id = '{$sids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdsRslt = db_exec($sql); # get accounts db_conn("exten"); $sql = "SELECT stkacc,cosacc FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); $stockacc = $wh['stkacc']; $cosacc = $wh['cosacc']; # dt(stock) ct(cos) writetrans($stockacc, $cosacc, $td, $refnum, $cosamt, "Cost Of Sales for Credit note No. {$notenum}."); db_conn($prd); # insert invoice items $sql = "INSERT INTO inv_note_items(noteid, whid, stkid, qty, amt, div, vatcode) \n\t\t\t\t\tVALUES('{$noteid}', '{$stkd['whid']}', '{$stkids[$keys]}', '{$qtys[$keys]}', \n\t\t\t\t\t\t'{$amt[$keys]}', '" . USER_DIV . "', '{$vatcode[$keys]}')"; $rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.", SELF); db_connect(); $date = date("Y-m-d"); $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div)\n\t\t\tVALUES('{$rodate}', '{$noteid}', '{$notenum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'nstk', '" . USER_DIV . "')"; $recRslt = db_exec($sql); } else { db_connect(); ###################VAT CALCS####################### $noted = $stkd['noted'] + $qtys[$keys]; db_conn($prd); $sql = "UPDATE pinv_items SET noted = '{$noted}' WHERE id = '{$sids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdsRslt = db_exec($sql); db_conn($prd); # insert invoice items $sql = "INSERT INTO inv_note_items(noteid, whid, stkid, qty, amt, div,description, vatcode) \n\t\t\t\t\tVALUES('{$noteid}', '{$stkd['vatcode']}', '{$stkids[$keys]}', '{$qtys[$keys]}', '{$amt[$keys]}', '" . USER_DIV . "', '{$stkd['description']}', '{$vatcode[$keys]}')"; $rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.", SELF); db_connect(); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd["vat_amount"]); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $av -= $ivat; $at -= $iamount; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Credit note No. {$notenum} for Customer : {$inv['cusname']} {$inv['surname']}", -$iamount, -$ivat); #################################################### $amtexvat = sprint($stkd['amt']); db_connect(); $sdate = date("Y-m-d"); $nsp += sprint($iamount - $ivat); if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = 0; } $commision = $commision + coms($inv['salespn'], $amt[$keys], $itemcommission); // //writetrans($cosacc, $stockacc,$inv['odate'] , $refnum, $cosamt, "Cost Of Sales for Invoice No.$invnum for Customer : $inv[cusname] $inv[surname]"); // writetrans($dept['debtacc'], $stkd['account'],$inv['odate'], $refnum, ($iamount-$ivat), "Debtors Control for Invoice No.$invnum for Customer : $inv[cusname] $inv[surname]"); db_connect(); $date = date("Y-m-d"); $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div)\n\t\t\tVALUES('{$rodate}', '{$noteid}', '{$notenum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'nnon', '" . USER_DIV . "')"; $recRslt = db_exec($sql); if ($inv['pcash'] > 0) { $min = $ro; $inv['pcash'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcash'] >= $ivat) { writetrans($vatacc, $dept['pca'], $td, $refnum, $ivat, "VAT Returned for Credit note No. {$notenum}"); $inv['pcash'] = sprint($inv['pcash'] - $ivat); $ivat = 0; if ($inv['pcash'] > 0) { if ($inv['pcash'] >= $iamount) { writetrans($stkd['account'], $dept['pca'], $td, $refnum, $iamount, "Sales for Credit note No. {$notenum}"); $inv['pcash'] = sprint($inv['pcash'] - $iamount); $iamount = 0; } elseif ($inv['pcash'] < $iamount) { writetrans($stkd['account'], $dept['pca'], $td, $refnum, $inv['pcash'], "Sales for Credit note No. {$notenum}"); $iamount = sprint($iamount - $inv['pcash']); $inv['pcash'] = 0; } } } else { writetrans($vatacc, $dept['pca'], $td, $refnum, $inv['pcash'], "VAT Returned for Credit note No. {$notenum}"); $ivat = sprint($ivat - $inv['pcash']); $inv['pcash'] = 0; } // db_conn('cubit'); // // $inv['pcash']-=$min; // // $Sl="INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('$sd','".USER_NAME."','$invnum','$inv[pcash]','Cash','".PRD_DB."','0')"; // $Ri=db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcheque'] > 0) { $min = $ro; $inv['pcheque'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcheque'] >= $ivat) { writetrans($vatacc, $dept['pca'], $td, $refnum, $ivat, "VAT Returned for Credit note No. {$notenum}"); $inv['pcheque'] = sprint($inv['pcheque'] - $ivat); $ivat = 0; if ($inv['pcheque'] > 0) { if ($inv['pcheque'] >= $iamount) { writetrans($stkd['account'], $dept['pca'], $td, $refnum, $iamount, "Sales for Credit note No. {$notenum}"); $inv['pcheque'] = sprint($inv['pcheque'] - $iamount); $iamount = 0; } elseif ($inv['pcheque'] < $iamount) { writetrans($stkd['account'], $dept['pca'], $td, $refnum, $inv['pcheque'], "Sales for Credit note No. {$notenum}"); $iamount = sprint($iamount - $inv['pcheque']); $inv['pcheque'] = 0; } } } else { writetrans($vatacc, $dept['pca'], $td, $refnum, $inv['pcheque'], "VAT Returned for Credit note No. {$notenum}"); $ivat = sprint($ivat - $inv['pcheque']); $inv['pcheque'] = 0; } db_conn('cubit'); $inv['pcash'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$sd}','" . USER_NAME . "','{$invnum}','-{$inv['pcash']}','Cash','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcc'] > 0) { db_conn('core'); $Sl = "SELECT * FROM salacc WHERE name='cc'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set a link for the POS credit card control account"; } $cd = pg_fetch_array($Ri); $cc = $cd['accnum']; $min = $ro; $inv['pcc'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcc'] >= $ivat) { writetrans($vatacc, $cc, $td, $refnum, $ivat, "VAT Returned for Credit note No. {$notenum}"); $inv['pcc'] = sprint($inv['pcc'] - $ivat); $ivat = 0; if ($inv['pcc'] > 0) { if ($inv['pcc'] >= $iamount) { writetrans($stkd['account'], $cc, $td, $refnum, $iamount, "Sales for Credit note No. {$notenum}"); $inv['pcc'] = sprint($inv['pcc'] - $iamount); $iamount = 0; } elseif ($inv['pcc'] < $iamount) { writetrans($stkd['account'], $cc, $td, $refnum, $inv['pcc'], "Sales for Credit note No. {$notenum}"); $iamount = sprint($iamount - $inv['pcc']); $inv['pcc'] = 0; } } } else { writetrans($vatacc, $cc, $td, $refnum, $inv['pcc'], "VAT Returned for Credit note No. {$notenum}"); $ivat = sprint($ivat - $inv['pcc']); $inv['pcc'] = 0; } db_conn('cubit'); $inv['pcash'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$sd}','" . USER_NAME . "','{$invnum}','-{$inv['pcash']}','Cash','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcredit'] > 0) { db_conn('core'); $min = $ro; $inv['pcredit'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcredit'] >= $ivat) { writetrans($vatacc, $dept['debtacc'], $td, $refnum, $ivat, "VAT Returned for Credit note No. {$notenum}"); $inv['pcredit'] = sprint($inv['pcredit'] - $ivat); $ivat = 0; if ($inv['pcredit'] > 0) { if ($inv['pcredit'] >= $iamount) { writetrans($stkd['account'], $dept['debtacc'], $td, $refnum, $iamount, "Sales for Credit note No. {$notenum}"); $inv['pcredit'] = sprint($inv['pcredit'] - $iamount); $iamount = 0; } elseif ($inv['pcredit'] < $iamount) { writetrans($stkd['account'], $dept['debtacc'], $td, $refnum, $inv['pcredit'], "Sales for Credit note No. {$notenum}"); $iamount = sprint($iamount - $inv['pcredit']); $inv['pcredit'] = 0; } } } else { writetrans($vatacc, $dept['debtacc'], $td, $refnum, $inv['pcredit'], "VAT Returned for Credit note No. {$notenum}"); $ivat = sprint($ivat - $inv['pcredit']); $inv['pcredit'] = 0; } db_conn('cubit'); $inv['pcash'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$sd}','" . USER_NAME . "','{$invnum}','-{$inv['pcash']}','Cash','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } } } db_connect(); # save invoice discount $sql = "INSERT INTO inv_discs(cusnum, invid, traddisc, itemdisc, inv_date, delchrg, div) VALUES('{$inv['cusnum']}', '{$invid}', '0', '-{$discs}', '{$inv['odate']}', '0', '" . USER_DIV . "')"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); /* - Start Transactoins - */ ###################VAT CALCS####################### db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE del='Yes'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { $Sl = "SELECT * FROM vatcodes"; $Ri = db_exec($Sl); } $vd = pg_fetch_array($Ri); $excluding = ""; $vr = vatcalc($delexvat, $inv['chrgvat'], $excluding, $inv['traddisc'], $vd["vat_amount"]); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $td, "OUTPUT", $vd['code'], $refnum, "VAT for Credit note No. {$notenum}, Customer : {$inv['cusname']} {$inv['surname']}", -$iamount, -$ivat); #################################################### /* # dt(income) ct(debtors) writetrans($dept['pia'], $dept['pca'], $td, $refnum, ($TOTAL-$VAT), "Debtors Control for Credit note No. $notenum for Customer : $inv[cusname] $inv[surname]"); # dt(vat) ct(debtors) writetrans($vatacc, $dept['pca'], $td, $refnum, $VAT, "VAT Return for Credit note No. $notenum for Customer : $inv[cusname] $inv[surname]"); */ // db_connect(); // $date = date("Y-m-d"); // $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div) // VALUES('$rodate', '$noteid', '$notenum', '$dept[debtacc]', '$VAT', '$TOTAL', 'nstk', '".USER_DIV."')"; // $recRslt = db_exec($sql); $Sl = "INSERT INTO sj(cid,name,des,date,exl,vat,inc,div) VALUES\n\t('{$inv['cusnum']}','{$inv['surname']}','Credit Note:{$notenum}, POS Invoice {$inv['invnum']}','{$rodate}','" . -sprint($TOTAL - $VAT) . "','-{$VAT}','" . -sprint($TOTAL) . "','" . USER_DIV . "')"; $Ri = db_exec($Sl); // $av=$VAT; // $at=$TOTAL-$VAT; // // $inv['pcash']=$pcash; // $inv['pcheque']=$pcheque; // $inv['pcc']=$pcc; // $inv['pcredit']=$pcredit; // // $sd=date("Y-m-d"); // // db_conn('cubit'); // $Sl="SELECT * FROM payrec WHERE inv='$invnum'"; // $Ri=db_exec($Sl); // // $data=pg_fetch_array($Ri); // // $user=$data['by']; // // $ro=$rounding; // $ro+=0; if ($inv['pcash'] > 0) { $min = $ro; $inv['pcash'] += $ro; $ro = 0; $amount = $inv['pcash']; if ($amount >= $av) { writetrans($vatacc, $dept['pca'], $td, $refnum, $av, "VAT Returned for POS Credit note: {$notenum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['pia'], $dept['pca'], $td, $refnum, $amount, "Sales for POS Credit note: {$notenum}."); $at = $at - $amount; } } else { writetrans($vatacc, $dept['pca'], $td, $refnum, $amount, "VAT Returned for POS Credit note: {$notenum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcash'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','{$user}','{$invnum}','-{$inv['pcash']}','Cash','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcheque'] > 0) { $min = $ro; $inv['pcheque'] += $ro; $ro = 0; $amount = $inv['pcheque']; if ($amount >= $av) { writetrans($vatacc, $dept['pca'], $td, $refnum, $av, "VAT Returned for POS Credit note: {$notenum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['pia'], $dept['pca'], $td, $refnum, $amount, "Sales for POS Credit note: {$notenum}."); $at = $at - $amount; } } else { writetrans($vatacc, $dept['pca'], $td, $refnum, $amount, "VAT Returned for POS Credit note: {$notenum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcheque'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','{$user}','{$invnum}','-{$inv['pcheque']}','Cheque','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcc'] > 0) { db_conn('core'); $Sl = "SELECT * FROM salacc WHERE name='cc'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set a link for the POS credit card control account"; } $cd = pg_fetch_array($Ri); $cc = $cd['accnum']; $min = $ro; $inv['pcc'] += $ro; $ro = 0; $amount = $inv['pcc']; if ($amount >= $av) { writetrans($vatacc, $cc, $td, $refnum, $av, "VAT Returned for POS Credit note: {$notenum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['pia'], $cc, $td, $refnum, $amount, "Sales for POS Credit note: {$notenum}."); $at = $at - $amount; } } else { writetrans($vatacc, $cc, $td, $refnum, $amount, "VAT Returned for POS Credit note: {$notenum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcc'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','-{$inv['pcc']}','Credit Card','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcredit'] > 0) { db_conn('core'); $cc = $dept['debtacc']; $min = $ro; $inv['pcredit'] += $ro; $ro = 0; $amount = $inv['pcredit']; if ($amount >= $av) { writetrans($vatacc, $cc, $td, $refnum, $av, "VAT Returned for POS Credit note: {$notenum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['pia'], $cc, $td, $refnum, $amount, "Sales for POS Credit note: {$notenum}."); $at = $at - $amount; } } else { writetrans($vatacc, $cc, $td, $refnum, $amount, "VAT Returned for POS Credit note: {$notenum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcc'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','-{$inv['pcredit']}','Credit','" . PRD_DB . "','{$noteid}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['rounding'] > 0) { if ($inv['pcash'] > 0) { writetrans($dept['pca'], $rac, $td, $refnum, $inv['rounding'], "Rounding on Credit note: {$notenum}."); } elseif ($inv['pcheque'] > 0) { writetrans($dept['pca'], $rac, $td, $refnum, $inv['rounding'], "Rounding on Credit note: {$notenum}."); } elseif ($inv['pcc'] > 0) { writetrans($cc, $rac, $td, $refnum, $inv['rounding'], "Rounding on Credit note: {$notenum}."); } elseif ($inv['pcredit'] > 0) { writetrans($dept['debtacc'], $rac, $td, $refnum, $inv['rounding'], "Rounding on Credit note: {$notenum}."); } } com_invoice($inv['salespn'], -($TOTAL - $VAT), -$commision, $invnum, $td); db_conn('cubit'); $Sl = "INSERT INTO pr(userid,username,amount,pdate,inv,cust,t) VALUES ('" . USER_ID . "','" . USER_NAME . "','-{$TOTAL}','{$td}','{$invnum}','{$inv['cusname']}','{$inv['terms']}')"; $Ry = db_exec($Sl) or errDie("Unable to insert pos record."); if ($rounding > 0) { $Sl = "INSERT INTO pcnc (note,amount) VALUES ('{$notenum}','{$rounding}')"; $Ri = db_exec($Sl); } $inv['pcredit'] = $pcreditback; if ($inv['cusnum'] > 0 && $inv['pcredit'] > 0) { $nt = $inv['pcredit']; # Record the payment on the statement $sql = "\n\t\t\tINSERT INTO stmnt \n\t\t\t\t(cusnum, invid, docref, amount, date, type, div, allocation_date) \n\t\t\tVALUES \n\t\t\t\t('{$inv['cusnum']}', '{$invnum}', '0', '-{$nt}', '{$inv['odate']}', 'Credit Note {$notenum}', '" . USER_DIV . "', '{$inv['odate']}')"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # Update the customer (make balance more) $sql = "UPDATE customers SET balance = (balance - '{$nt}') WHERE cusnum = '{$inv['cusnum']}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); # Update the invoice (make balance less) $sql = "UPDATE open_stmnt SET balance = balance-'{$pcreditback}' WHERE invid = '{$inv['invnum']}'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); custledger($inv['cusnum'], $dept['incacc'], $inv['odate'], $invnum, "Credit note {$notenum}", $nt, "c"); //print $nt;exit; recordCT($nt, $inv['cusnum'], $inv['odate']); } pglib_transaction("COMMIT"); //die("<br /><br />NOTE: TRANSACTION ROLLBACK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); /* - End Transactoins - */ /* -- Final Layout -- */ $details = "\n\t\t\t\t\t<center>\n\t\t\t\t\t<h2>Credit Note</h2>\n\t\t\t\t\t<table cellpadding='0' cellspacing='4' border=0 width='750'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td valign='top' width='30%'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td>{$inv['surname']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='top' width='25%'>\n\t\t\t\t\t\t\t\t" . COMP_NAME . "<br>\n\t\t\t\t\t\t\t\t" . COMP_ADDRESS . "<br>\n\t\t\t\t\t\t\t\t" . COMP_TEL . "<br>\n\t\t\t\t\t\t\t\t" . COMP_FAX . "<br>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td width='20%'>\n\t\t\t\t\t\t\t\t<img src='compinfo/getimg.php' width='230' height='47'>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='bottom' align='right' width='25%'>\n\t\t\t\t\t\t\t\t<table cellpadding='2' cellspacing='0' border='1' bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Credit Note No.</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$notenum}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Invoice No.</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$inv['invnum']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Order No.</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$inv['ordno']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Terms</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$terms} Days</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Credit note Date</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$rodate}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='4'>\n\t\t\t\t\t\t\t\t<table cellpadding='5' cellspacing='0' border='1' width='100%' bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t\t\t\t\t\t\t<th width='45%'>DESCRIPTION</th>\n\t\t\t\t\t\t\t\t\t\t<th>QTY RETURNED</th>\n\t\t\t\t\t\t\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t\t\t\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t{$products}\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<table border='1' cellspacing='0' bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td>" . nl2br($comm) . "</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align='right' colspan='3'>\n\t\t\t\t\t\t\t\t<table cellpadding='5' cellspacing='0' border='1' width='50%' bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>SUBTOTAL</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$SUBTOT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Trade Discount</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$traddiscmt}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Delivery Charge</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$delexvat}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>VAT @ {$VATP}%</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th><b>GRAND TOTAL<b></th>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " border='1'>\n\t\t\t\t\t\t \t<tr>\n\t\t\t\t\t\t \t\t<th>VAT No.</th>\n\t\t\t\t\t\t \t\t<td align='center'>" . COMP_VATNO . "</td>\n\t\t\t\t\t\t \t</tr>\n\t\t\t\t\t\t </table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td><br></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t</center>"; $OUTPUT = "<script>printer2('pos-note-slip.php?noteid={$noteid}&prd={$prd}&cccc=true');move('main.php');</script>"; require "template.php"; }
function write($_POST) { # get vars extract($_POST); if (isset($back)) { return details($_POST); } # validate input require_lib("validate"); $v = new validate(); $v->isOk($invid, "num", 1, 20, "Invalid invoice number."); $v->isOk($comm, "string", 0, 255, "Invalid Comments."); $v->isOk($terms, "num", 1, 20, "Invalid terms."); $v->isOk($odate, "date", 1, 14, "Invalid Invoice note date."); $v->isOk($traddisc, "float", 0, 20, "Invalid Trade Discount."); $v->isOk($delchrg, "float", 0, 20, "Invalid Delivery Charge."); $v->isOk($SUBTOT, "float", 0, 20, "Invalid Delivery Charge."); # used to generate errors $error = "asa@"; # check quantities if (isset($qtys)) { foreach ($qtys as $keys => $qty) { $v->isOk($qty, "float", 1, 15, "Invalid Returned Quantity."); $v->isOk($disc[$keys], "float", 0, 20, "Invalid Discount."); $v->isOk($discp[$keys], "float", 0, 20, "Invalid Discount Percentage."); } } else { $v->isOk($error, "num", 0, 1, "Invalid Returned Quantity."); } # check stkids[] if (isset($stkids)) { foreach ($stkids as $keys => $stkid) { $v->isOk($stkid, "num", 1, 10, "Invalid Stock number, please enter all details."); } } else { $v->isOk($error, "num", 0, 1, "Invalid Stock number, please enter all details."); } # check amt[] if (isset($amt)) { foreach ($amt as $keys => $amount) { $v->isOk($amount, "float", 1, 20, "Invalid Amount, please enter all details."); } } else { $v->isOk($error, "num", 0, 1, "Invalid Amount, please enter all details."); } # display errors, if any $err = ""; if ($v->isError()) { $errors = $v->getErrors(); foreach ($errors as $e) { $err .= "<li class='err'>" . $e["msg"] . "</li>"; } return details($_POST, $err); } /* -------------------------------- */ # Get invoice info db_connect(); $sql = "SELECT * FROM invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $invRslt = db_exec($sql) or errDie("Unable to get invoice information"); if (pg_numrows($invRslt) < 1) { return "<i class='err'>Not Found</i>"; } $inv = pg_fetch_array($invRslt); pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF); $notenum = divlastid('note', USER_DIV); /* --- Start Products Display --- */ $vatamount = 0; # Products layout $products = ""; $taxex = 0; $ai = 0; $amt = array(); foreach ($qtys as $keys => $value) { db_connect(); # get selamt from selected stock $sql = "SELECT * FROM stock WHERE stkid = '{$stkids[$keys]}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); # get selected stock in this invoice $sql = "SELECT * FROM inv_items WHERE id = '{$ids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $stkd = pg_fetch_array($stkdRslt); if ($stkd['account'] == 0) { # get warehouse name db_conn("exten"); $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); # Calculate the Discount discount if ($disc[$keys] < 1) { if ($discp[$keys] > 0) { $disc[$keys] = $discp[$keys] / 100 * $stkd['unitcost']; } } else { $discp[$keys] = $disc[$keys] * 100 / $stkd['unitcost']; } # Calculate amount $amt[$keys] = $qtys[$keys] * ($stkd['unitcost'] - $disc[$keys]); db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); # Check Tax Excempt if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $taxex += $amt[$keys]; } if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $vatamount += $ivat; if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } db_conn("exten"); # put in product $products .= "\n\t\t\t\t<tr>\n\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stk['stkid']}'>{$stk['stkcod']}</td>\n\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t<td><input type='hidden' size='5' name='qtys[{$ai}]' value='{$qtys[$keys]}'>{$qtys[$keys]}</td>\n\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[{$ai}]' value='{$amt[$keys]}'>" . CUR . " {$amt[$keys]}</td>\n\t\t\t\t</tr>"; ++$ai; } else { # get warehouse name db_conn("core"); $sql = "SELECT accname FROM accounts WHERE accid = '{$stkd['account']}'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); $discp[$keys] = 0; # Calculate amount $amt[$keys] = $qtys[$keys] * ($stkd['unitcost'] - $disc[$keys]); $nons_amt[$keys] = $qtys[$keys] * ($stkd['unitcost'] - $disc[$keys]); # Check Tax Excempt if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $taxex += $amt[$keys]; } db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $vatamount += $ivat; if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } db_conn("exten"); $wh['whname'] = ""; $stk['stkid'] = 0; $stk['stkcod'] = $wh['accname']; $stk['stkdes'] = $stkd['description']; # put in product $products .= "\n\t\t\t\t<tr>\n\t\t\t\t\t<td><input type='hidden' name='stkids[{$ai}]' value='{$stk['stkid']}'>{$stk['stkcod']}</td>\n\t\t\t\t\t<td>{$stk['stkdes']}</td>\n\t\t\t\t\t<td><input type='hidden' size='5' name='qtys[{$ai}]' value='{$qtys[$keys]}'>{$qtys[$keys]}</td>\n\t\t\t\t\t<td nowrap>" . CUR . " {$stkd['unitcost']}</td>\n\t\t\t\t\t<td nowrap><input type='hidden' name='amt[{$ai}]' value='{$amt[$keys]}'>" . CUR . " {$amt[$keys]}</td>\n\t\t\t\t</tr>"; ++$ai; } } # get department db_conn("exten"); $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'"; $deptRslt = db_exec($sql); if (pg_numrows($deptRslt) < 1) { $dept['deptname'] = "<i class=err>Not Found</i>"; } else { $dept = pg_fetch_array($deptRslt); } db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$inv['delvat']}'"; $Ri = db_exec($Sl); $vd = pg_fetch_array($Ri); if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($delchrg, $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $DELVAT = $vrs[0]; if ($vd['zero'] == "Yes") { $DELVAT = 0; } // db_conn('cubit'); // $Sl="SELECT * FROM vatcodes WHERE id='$inv[delvat]'"; // $Ri=db_exec($Sl); // // $vd=pg_fetch_array($Ri); // // $vr=vatcalc($amt[$keys],$inv['chrgvat'],$excluding,$inv['traddisc'],$vd['vat_amount']); // $vrs=explode("|",$vr); // $ivat=$vrs[0]; // $iamount=$vrs[1]; // // $vatamount += $ivat; // if(pg_num_rows($Ri)>0) { // $taxex += $delchrg; // } /* --- ----------- Clac --------------------- # calculate subtot $SUBTOT = 0.00; if(isset($amt)) $SUBTOT = array_sum($amt); $SUBTOT -= $taxex; # duplicate $SUBTOTAL = $SUBTOT; $VATP = TAX_VAT; if($inv['chrgvat'] == "exc"){ $SUBTOTAL = $SUBTOTAL; $delexvat= ($delchrg); }elseif($inv['chrgvat'] == "inc"){ $SUBTOTAL = sprint(($SUBTOTAL * 100)/(100 + $VATP)); $delexvat = sprint(($delchrg * 100)/($VATP + 100)); }else{ $SUBTOTAL = ($SUBTOTAL); $delexvat = ($delchrg); } $SUBTOT = $SUBTOTAL; $EXVATTOT = $SUBTOT; $EXVATTOT += $delexvat; # Minus trade discount from taxex if($traddisc > 0){ $traddiscmtt = (($traddisc/100) * $taxex); }else{ $traddiscmtt = 0; } $taxext = ($taxex - $traddiscmtt); if($traddisc > 0) { $traddiscmt = ($EXVATTOT * ($traddisc/100)); }else{ $traddiscmt = 0; } $EXVATTOT -= $traddiscmt; // $EXVATTOT -= $taxex; $traddiscmt = sprint($traddiscmt + $traddiscmtt); if($inv['chrgvat'] != "nov"){ $VAT = sprint($EXVATTOT * ($VATP/100)); }else{ $VAT = 0; } $TOTAL = sprint($EXVATTOT + $VAT + $taxext); $SUBTOT += $taxex; /* --- ----------- Clac --------------------- */ # Get invoice info db_connect(); $sql = "SELECT * FROM invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $invRslt = db_exec($sql) or errDie("Unable to get invoice information"); if (pg_numrows($invRslt) < 1) { return "<li class='err'>Invoice Not Found</li>"; } $inv = pg_fetch_array($invRslt); /* A quick fix by jupiter $allnoted = true; foreach($qtys as $keys => $value) { $sql = "SELECT * FROM inv_items WHERE id = '$ids[$keys]' AND invid ='$invid' AND div = '".USER_DIV."'"; $stkdRslt = db_exec($sql); $stkd = pg_fetch_array($stkdRslt); if($stkd['qty'] != $qtys[$keys]){ $allnoted = false; } } if($allnoted){ $SUBTOT = sprint($inv['subtot']); $VAT = sprint($inv['vat']); $TOTAL = sprint($inv['total']); $delchrg = sprint($inv['delivery']); $traddiscmt = sprint($inv['discount']); $SUBTOTAL = sprint($TOTAL - $VAT); } /* End A quick fix by jupiter */ /* --- ----------- Clac --------------------- */ ##----------------------NEW---------------------- $sub = 0.0; if (isset($amt)) { $sub = sprint(array_sum($amt)); } $nons_total = 0.0; if (isset($nons_amt)) { $nons_total = sprint(array_sum($nons_amt)); } $VATP = TAX_VAT; if ($inv['chrgvat'] == "exc") { $taxex = sprint($taxex - $taxex * $traddisc / 100); $subtotal = sprint($sub + $delchrg); $traddiscmt = sprint($subtotal * $traddisc / 100); $subtotal = sprint($subtotal - $traddiscmt); // $VAT=sprint(($subtotal-$taxex)*$VATP/100); $VAT = sprint($vatamount + $DELVAT); $SUBTOT = $sub; $TOTAL = sprint($subtotal + $VAT); $delexvat = sprint($delchrg); } elseif ($inv['chrgvat'] == "inc") { $ot = $taxex; $taxex = sprint($taxex - $taxex * $traddisc / 100); $subtotal = sprint($sub + $delchrg); $traddiscmt = sprint($subtotal * $traddisc / 100); $subtotal = sprint($subtotal - $traddiscmt); //$VAT=sprint(($subtotal-$taxex)*$VATP/(100+$VATP)); $VAT = sprint($vatamount + $DELVAT); $TOTAL = sprint($subtotal); //$SUBTOT=sprint($TOTAL - $VAT - ($delchrg - $DELVAT)); $SUBTOT = sprint($sub); $delexvat = sprint($delchrg); $traddiscmt = sprint($traddiscmt); } else { $subtotal = sprint($sub + $delchrg); $traddiscmt = sprint($subtotal * $traddisc / 100); $subtotal = sprint($subtotal - $traddiscmt); $VAT = sprint(0); $SUBTOT = $sub; $TOTAL = $subtotal; $delexvat = sprint($delchrg); } /* --- ----------- Clac --------------------- */ ##----------------------END---------------------- if ($inv['balance'] >= $TOTAL) { $invpay = $TOTAL; $examt = 0; } else { $invpay = $inv['balance']; $examt = $TOTAL - $invpay; } /* - Start Hooks - */ $vatacc = gethook("accnum", "salesacc", "name", "VAT", "z"); /* - End Hooks - */ # Todays date $date = date("d-m-Y"); $sdate = date("Y-m-d"); $td = $odate; $refnum = getrefnum(); /*refnum*/ # Insert invoice to period DB db_conn($inv['prd']); # Insert invoice credit note to DB $sql = "\n\t\tINSERT INTO inv_notes (\n\t\t\tdeptid, notenum, invnum, invid, cusnum, cordno, ordno, \n\t\t\tchrgvat, terms, traddisc, salespn, odate, delchrg, subtot, vat, \n\t\t\ttotal, comm, username, div, surname, cusaddr, cusvatno, \n\t\t\tdeptname, branch, bankid, prd\n\t\t) VALUES (\n\t\t\t'{$inv['deptid']}', '{$notenum}', '{$inv['invnum']}', '{$inv['invid']}', '{$inv['cusnum']}', '{$inv['cordno']}', '{$inv['ordno']}', \n\t\t\t'{$inv['chrgvat']}', '{$terms}', '{$traddiscmt}', '{$inv['salespn']}', '{$odate}', '{$delexvat}', '{$SUBTOT}', '{$VAT}' , \n\t\t\t'{$TOTAL}', '{$comm}', '" . USER_NAME . "', '" . USER_DIV . "', '{$inv['surname']}', '{$inv['cusaddr']}', '{$inv['cusvatno']}', \n\t\t\t'{$inv['deptname']}', '{$inv['branch']}', '{$inv['bankid']}', '{$inv['prd']}'\n\t\t)"; $rslt = db_exec($sql) or errDie("Unable to insert invoice to Cubit.", SELF); # Get next ordnum $noteid = pglib_lastid("inv_notes", "noteid"); db_connect(); # Begin updating $nbal = $inv['nbal'] + $TOTAL; # Update the invoice (make balance less) $sql = "UPDATE invoices SET nbal = '{$nbal}', rdelchrg = (rdelchrg + '{$delchrg}'), balance = balance - '{$invpay}' WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); # Update the invoice (make balance less) $sql = "UPDATE open_stmnt SET balance = balance-'{$TOTAL}' WHERE invid = '{$inv['invnum']}'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); # Update the customer (make balance less) $sql = "UPDATE customers SET balance = (balance - '{$TOTAL}') WHERE cusnum = '{$inv['cusnum']}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); # Update invoice's discounts # $sql = "UPDATE inv_discs SET traddisc = (traddisc - '$traddiscm'), itemdisc = (itemdisc - '$discs') WHERE cusnum = '$inv[cusnum]' AND invid = '$invid'"; # $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.",SELF); # record the payment on the statement $sql = "\n\t\tINSERT INTO stmnt (\n\t\t\tcusnum, invid, amount, date, \n\t\t\ttype, div, allocation_date\n\t\t) VALUES (\n\t\t\t'{$inv['cusnum']}', '{$notenum}', '" . ($TOTAL - $TOTAL * 2) . "', '{$odate}', \n\t\t\t'Credit Note for invoice No. {$inv['invnum']}', '" . USER_DIV . "', '{$odate}'\n\t\t)"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); $disc = 0; $commision = 0; # Commit updating $nsp = 0; # Make ledge record custledger($inv['cusnum'], $dept['incacc'], $td, $notenum, "Credit Note No. {$notenum} for invoice No. {$inv['invnum']}", $TOTAL, "c"); $salesp = qrySalesPersonN($inv["salespn"]); if ($examt > 0) { # Make record for age analisys custCTP($examt, $inv['cusnum'], $td); } #recalculate the total cost amount for ONLY THE CREDITED ITEMS $ntcosamt = 0; foreach ($qtys as $keys => $value) { db_connect(); # get selamt from selected stock $sql = "SELECT * FROM stock WHERE stkid = '{$stkids[$keys]}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); # get selected stock in this invoice $sql = "SELECT * FROM inv_items WHERE id = '{$ids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $stkd = pg_fetch_array($stkdRslt); if ($stkd['account'] == 0) { # Keep track of discounts $disc += $stkd['disc'] * $stkd['qty']; db_connect(); $Sl = "SELECT * FROM scr WHERE inv='{$inv['invnum']}' AND stkid='{$stkd['stkid']}' AND invid = '{$stkd['id']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) > 0) { $cd = pg_fetch_array($Ri); $stk['csprice'] = $cd['amount']; } else { $stk['csprice'] = 0; } # cost amount if ($stk['csprice'] == "0.00") { $cosamt = sprint($qtys[$keys] * $stk['lcsprice']); } else { $cosamt = sprint($qtys[$keys] * $stk['csprice']); } #add this cost amount to the new total $ntcosamt += $cosamt; db_connect(); # Update stock(onhand + qty) $sql = "UPDATE stock SET csamt = (csamt + '{$cosamt}'), units = (units + '{$qtys[$keys]}') WHERE stkid = '{$stkids[$keys]}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update stock to Cubit.", SELF); # fix stock cost amount $Sl = "UPDATE stock set csprice=csamt/units WHERE stkid = '{$stkids[$keys]}' AND units>0"; $Ri = db_exec($Sl) or errDie("Unable to update stock cost price in Cubit.", SELF); if ($stk['serd'] == 'yes') { ext_InSer($stkd['serno'], $stkd['stkid'], "{$inv['cusname']} {$inv['surname']}", $notenum, 'note', $td); } # negetive values to minus profit $nqty = $qtys[$keys] * 1; $namt = $amt[$keys] * -1; $ncsprice = $cosamt * -1; $noted = $stkd['noted'] + $qtys[$keys]; # stkid, stkcod, stkdes, trantype, edate, qty, csamt, details stockrec($stkd['stkid'], $stk['stkcod'], $stk['stkdes'], 'dt', $td, $nqty, $cosamt, "Credit note for Customer : {$inv['surname']} - Credit note No. {$notenum}"); # Get amount exluding vat if including and not exempted $VATP = TAX_VAT; $amtexvat = $amt[$keys]; ###################VAT CALCS####################### db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $vatamount += $ivat; vatr($vd['id'], $td, "OUTPUT", $vd['code'], $refnum, "VAT for Credit note: {$notenum} Customer : {$inv['cusname']} {$inv['surname']}", -$iamount, -$ivat); if ($excluding == "y") { $exvatamt = $iamount; } else { $exvatamt = $iamount - $ivat; } #################################################### $sql = "\n\t\t\t\tINSERT INTO stockrec (\n\t\t\t\t\tedate, stkid, stkcod, stkdes, trantype, qty, csprice, \n\t\t\t\t\tcsamt, details, div\n\t\t\t\t) VALUES (\n\t\t\t\t\t'{$td}', '{$stk['stkid']}', '{$stk['stkcod']}', '{$stk['stkdes']}', 'note', '{$qtys[$keys]}', '{$amtexvat}', \n\t\t\t\t\t'{$cosamt}', 'Credit note for Customer : {$inv['surname']} - Credit note No. {$notenum}', '" . USER_DIV . "'\n\t\t\t\t)"; $recRslt = db_exec($sql); # Get selected stock in this invoice $sql = "UPDATE inv_items SET noted = '{$noted}' WHERE id = '{$ids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdsRslt = db_exec($sql); $stkds = pg_fetch_array($stkdsRslt); # get accounts db_conn("exten"); $sql = "SELECT stkacc,cosacc FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); $stockacc = $wh['stkacc']; $cosacc = $wh['cosacc']; # sales rep commission if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = $stk["com"]; } $commision = $commision + coms($inv['salespn'], $exvatamt, $itemcommission); writetrans($stockacc, $cosacc, $td, $refnum, $cosamt, "Cost Of Sales for Credit note No. {$notenum} for Customer: {$inv['cusname']} {$inv['surname']}"); db_conn($inv['prd']); # insert invoice items $sql = "\n\t\t\t\tINSERT INTO inv_note_items (\n\t\t\t\t\tnoteid, whid, stkid, qty, amt, div, vatcode\n\t\t\t\t) VALUES (\n\t\t\t\t\t'{$noteid}', '{$stkd['whid']}', '{$stkids[$keys]}', '{$qtys[$keys]}', '{$amt[$keys]}', '" . USER_DIV . "', '{$stkd['vatcode']}'\n\t\t\t\t)"; $rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.", SELF); db_connect(); $sql = "\n\t\t\t\tINSERT INTO salesrec (\n\t\t\t\t\tedate, invid, invnum, debtacc, vat, total, typ, div\n\t\t\t\t) VALUES (\n\t\t\t\t\t'{$odate}', '{$noteid}', '{$notenum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'nstk', '" . USER_DIV . "'\n\t\t\t\t)"; $recRslt = db_exec($sql); } else { # Keep track of discounts //$disc += ($stkd['disc'] * $stkd['qty']); # negetive values to minus profit $nqty = $qtys[$keys] * 1; $namt = $amt[$keys] * -1; //$ncsprice = ($cosamt * (-1)); $noted = $stkd['noted'] + $qtys[$keys]; # Get amount exluding vat if including and not exempted $VATP = TAX_VAT; $amtexvat = $amt[$keys]; ###################VAT CALCS####################### $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($amt[$keys], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $vatamount += $ivat; vatr($vd['id'], $td, "OUTPUT", $vd['code'], $refnum, "VAT for Credit note: {$notenum} Customer : {$inv['cusname']} {$inv['surname']}", -$iamount, -$ivat); if ($excluding == "y") { $exvatamt = $iamount; } else { $exvatamt = $iamount - $ivat; } #################################################### if ($inv['chrgvat'] == "exc") { $nvat = $stkd['amt'] / 100 * $vd['vat_amount']; $ncosamt = round($stkd['qty'] * $stkd['amt'] + $nvat, 2); } else { $nvat = $stkd['amt'] / (100 + $vd['vat_amount']) * $vd['vat_amount']; $ncosamt = round($stkd['qty'] * $stkd['amt'], 2); } $ntcosamt += $ncosamt; #################################################### # Get selected stock in this invoice $sql = "UPDATE inv_items SET noted = '{$noted}' WHERE id = '{$ids[$keys]}' AND invid ='{$invid}' AND div = '" . USER_DIV . "'"; $stkdsRslt = db_exec($sql); $stkds = pg_fetch_array($stkdsRslt); $nsp += sprint($iamount - $ivat); //writetrans($cosacc, $stockacc,$inv['odate'] , $refnum, $cosamt, "Cost Of Sales for Invoice No.$invnum for Customer : $inv[cusname] $inv[surname]"); writetrans($stkd['account'], $dept['debtacc'], $td, $refnum, $iamount - $ivat, "Debtors control for Credit note: {$notenum} Customer : {$inv['cusname']} {$inv['surname']}"); //# dt(stock) ct(cos) // writetrans($stockacc, $cosacc, $td, $refnum, $cosamt, "Cost Of Sales for Credit note No. $notenum for Customer : $inv[cusname] $inv[surname]"); if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = $stk["com"]; } $commision = $commision + coms($inv['salespn'], $exvatamt, $itemcommission); db_conn($inv['prd']); # insert invoice items $sql = "\n\t\t\t\tINSERT INTO inv_note_items (\n\t\t\t\t\tnoteid, whid, stkid, qty, amt, div, description, vatcode\n\t\t\t\t) VALUES (\n\t\t\t\t\t'{$noteid}', '{$stkd['account']}', '0', '{$qtys[$keys]}', '{$amt[$keys]}', '" . USER_DIV . "', '{$stkd['description']}', '{$stkd['vatcode']}'\n\t\t\t\t)"; $rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.", SELF); db_connect(); $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div)\n\t\t\tVALUES('{$odate}', '{$noteid}', '{$notenum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'nnon', '" . USER_DIV . "')"; $recRslt = db_exec($sql); } } db_connect(); # save invoice discount $sql = "INSERT INTO inv_discs(cusnum, invid, traddisc, itemdisc, inv_date, delchrg, div) VALUES('{$inv['cusnum']}', '{$invid}', '0', '-{$disc}', '{$inv['odate']}', '0', '" . USER_DIV . "')"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); /* - Start Transactoins - */ // ###################VAT CALCS####################### // db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$inv['delvat']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { $Sl = "SELECT * FROM vatcodes"; $Ri = db_exec($Sl); } // $vd = pg_fetch_array($Ri); // $excluding = ""; // $vr = vatcalc($delexvat, $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; // // if($vd['zero']=="Yes") { // $ivat=0; // } // // $vatamount += $ivat; // vatr($vd['id'], $td, "OUTPUT", $vd['code'], $refnum, "VAT for Credit note No. {$notenum}, Customer : {$inv['cusname']} {$inv['surname']}", sprint(-$iamount - $ivat), -$ivat); // // #################################################### com_invoice($inv['salespn'], -($TOTAL - $VAT), -$commision, $inv['invnum'], $td, true); if ($TOTAL - $VAT - $nsp > 0) { # dt(income) ct(debtors) writetrans($dept['incacc'], $dept['debtacc'], $td, $refnum, $TOTAL - $VAT - $nsp, "Debtors Control for Credit note No. {$notenum} for Customer : {$inv['cusname']} {$inv['surname']}"); } # dt(vat) ct(debtors) writetrans($vatacc, $dept['debtacc'], $td, $refnum, $VAT, "VAT Return for Credit note No. {$notenum} for Customer : {$inv['cusname']} {$inv['surname']}"); db_connect(); // $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div) // VALUES('$odate', '$noteid', '$notenum', '$dept[debtacc]', '$VAT', '$TOTAL', 'nstk', '".USER_DIV."')"; // $recRslt = db_exec($sql); $Sl = "INSERT INTO sj(cid,name,des,date,exl,vat,inc,div) VALUES\n\t('{$inv['cusnum']}','{$inv['surname']}','Credit Note:{$notenum}, Invoice {$inv['invnum']}','{$odate}','" . -sprint($TOTAL - $VAT) . "','-{$VAT}','" . -sprint($TOTAL) . "','" . USER_DIV . "')"; $Ri = db_exec($Sl); pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF); /* - End Transactoins - */ $OUTPUT = "\n\t\t<script>\n\t\t\tsCostCenter('ct', 'Credit Note', '{$odate}', 'Credit Note for Invoice No.{$inv['invnum']} for Customer {$inv['cusname']} {$inv['surname']}', '" . ($TOTAL - $VAT - $nons_total) . "', 'Cost Of Sales for Credit Note for Invoice No.{$inv['invnum']}', '{$ntcosamt}', '');\n\t\t\tprinter('invoice-note-reprint.php?noteid={$noteid}&prd={$inv['prd']}&cccc=yes&reprint=no');\n\t\t\tmove('main.php');\n\t\t</script>"; require "template.php"; }
function details($_POST) { extract($_POST); # validate input require_lib("validate"); $v = new validate(); $v->isOk($invid, "num", 1, 20, "Invalid invoice number."); # display errors, if any if ($v->isError()) { $err = ""; $errors = $v->getErrors(); foreach ($errors as $e) { $err .= "<li class='err'>{$e['msg']}</li>"; } return $confirm; } db_connect(); # Get invoice info $sql = "SELECT * FROM invoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $invRslt = db_exec($sql) or errDie("Unable to get invoice information"); if (pg_numrows($invRslt) < 1) { return "<i class='err'>Not Found</i>"; } $inv = pg_fetch_array($invRslt); $sql = "SELECT stkid FROM inv_items WHERE invid = '{$inv['invid']}' AND div = '" . USER_DIV . "'"; $crslt = db_exec($sql); if (pg_numrows($crslt) < 1) { $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has no items.</li>"; $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>"; return $error; } # check if invoice has been printed if ($inv['printed'] == "y") { $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has already been printed.</li>"; return $error; } # check if invoice has been serialised if ($inv['serd'] == "n") { $error = "<li class='err'> Error : You must select serial numbers for some Items on Invoice No. <b>T {$invid}</b> before you can print it.</li>"; return $error; } #check if this transaction date is allowed // $curyr = getActiveFinYear(); // if ($yr > $curyr || ($yr == $curyr && $mon > $PRDMON[12])) { // $v->addError("", "Cannot do transaction in future financial year. ".(DEBUG>0?"\"$details\"":"")); // } db_conn('cubit'); $showvat = TRUE; $blocked_date_from = getCSetting("BLOCKED_FROM"); $blocked_date_to = getCSetting("BLOCKED_TO"); if (strtotime($inv['odate']) >= strtotime($blocked_date_from) and strtotime($inv['odate']) <= strtotime($blocked_date_to) and !user_is_admin(USER_ID)) { return "<li class='err'>Period Range Is Blocked. Only an administrator can process entries within this period.</li>"; } pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF); $invnum = divlastid('inv', USER_DIV); $Sl = "INSERT INTO ncsrec (oldnum, newnum, div) VALUES ('{$invid}', '{$invnum}', '" . USER_DIV . "')"; $Rs = db_exec($Sl) or errDie("Unable to insert into db"); # Get department db_conn("exten"); $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'"; $deptRslt = db_exec($sql); if (pg_numrows($deptRslt) < 1) { $dept['deptname'] = "<i class='err'>Not Found</i>"; } else { $dept = pg_fetch_array($deptRslt); } /* --- Start Products Display --- */ # Products layout $commision = 0; $products = array(); $disc = 0; # get selected stock in this invoice db_connect(); $sql = "SELECT * FROM inv_items WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $taxex = 0; $i = 0; $page = 0; $salesp = qrySalesPersonN($inv["salespn"]); while ($stkd = pg_fetch_array($stkdRslt)) { if ($i >= 25) { $page++; $i = 0; } $stkd['account'] += 0; if ($stkd['account'] == 0) { # get warehouse name db_conn("exten"); $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); # get selected stock in this warehouse db_connect(); $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") { $showvat = FALSE; } $sp = ""; # Check Tax Excempt if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $taxex += $stkd['amt']; $ex = "#"; } else { $ex = ""; } # all must be excempted if ($inv['chrgvat'] == 'nov') { $ex = "#"; } # Keep track of discounts $disc += $stkd['disc'] * $stkd['qty']; # Insert stock record $sdate = date("Y-m-d"); $csprice = sprint($stk['csprice'] * $stkd['qty']); # Sales rep commission if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = $stk["com"]; } #if this item is not exvat (ie. $ex != #) then reduce by the vat amount if ((strlen($ex) != "#" or $vd['vat_amount'] > 0) and $inv['chrgvat'] == "inc") { $vat = sprint($stkd['amt'] * $vd['vat_amount'] / (100 + $vd['vat_amount'])); $exvatamt = sprint($stkd['amt'] - $vat); } else { $exvatamt = sprint($stkd['amt']); } $commision = $commision + coms($inv['salespn'], sprint($exvatamt), $itemcommission); if (strlen($stkd['serno']) > 0) { $showser = "<br>" . trim($stkd['serno']); } else { $showser = ""; } # Put in product $products[$page][] = "\n\t\t\t\t<tr valign='top'>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$stk['stkcod']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$ex} {$sp} {$stk['stkdes']} {$showser}</td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$stkd['qty']} </td>\n\t\t\t\t\t<td align='right' style='border-right: 2px solid #000'>{$stkd['unitcost']} </td>\n\t\t\t\t\t<td align='right' style='border-right: 2px solid #000'>{$stkd['disc']} </td>\n\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$stkd['amt']} </td>\n\t\t\t\t</tr>"; $i++; } else { db_conn('core'); $Sl = "SELECT * FROM accounts WHERE accid='{$stkd['account']}'"; $Ri = db_exec($Sl) or errDie("Unable to get account data."); $ad = pg_fetch_array($Ri); db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") { $showvat = FALSE; } $sp = ""; # Check Tax Excempt if ($vd['zero'] == "Yes") { $taxex += $stkd['amt']; $ex = "#"; } else { $ex = ""; } # all must be excempted if ($inv['chrgvat'] == 'nov') { $ex = "#"; } #if this item is not exvat (ie. $ex != #) then reduce by the vat amount if ((strlen($ex) != "#" or $vd['vat_amount'] > 0) and $inv['chrgvat'] == "inc") { $vat = sprint($stkd['amt'] * $vd['vat_amount'] / (100 + $vd['vat_amount'])); $exvatamt = sprint($stkd['amt'] - $vat); } else { $exvatamt = sprint($stkd['amt']); } if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = 0; } $commision = $commision + coms($inv['salespn'], sprint($exvatamt), $itemcommission); # Put in product $products[$page][] = "\n\t\t\t\t<tr valign='top'>\n\t\t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$ex} {$sp} {$stkd['description']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$stkd['qty']} </td>\n\t\t\t\t\t<td align='right' style='border-right: 2px solid #000'>{$stkd['unitcost']} </td>\n\t\t\t\t\t<td align='right' style='border-right: 2px solid #000'>{$stkd['disc']} </td>\n\t\t\t\t\t<td align='right' nowrap>" . CUR . " {$stkd['amt']} </td>\n\t\t\t\t</tr>"; $i++; } } $blank_lines = 25; foreach ($products as $key => $val) { $bl = $blank_lines - count($products[$key]); for ($i = 0; $i <= $bl; $i++) { $products[$key][] = "\n\t \t\t\t<tr>\n\t \t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t \t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t \t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t \t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t \t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t \t\t\t\t<td> </td>\n\t \t\t\t</tr>"; } } /* --- Start Some calculations --- */ # Subtotal $SUBTOT = sprint($inv['subtot']); # Calculate tradediscm if (strlen($inv['traddisc']) > 0) { $traddiscm = sprint($inv['traddisc'] / 100 * $SUBTOT); } else { $traddiscm = "0.00"; } # Calculate subtotal $VATP = TAX_VAT; $SUBTOTAL = sprint($inv['subtot']); $VAT = sprint($inv['vat']); $TOTAL = sprint($inv['total']); $inv['delchrg'] = sprint($inv['delchrg']); com_invoice($inv['salespn'], $TOTAL - $VAT, sprint($commision), $invnum, $inv["odate"], true); /* --- End Some calculations --- */ /* - Start Hooks - */ $vatacc = gethook("accnum", "salesacc", "name", "VAT", "VAT"); /* - End Hooks - */ # Todays date $date = date("d-m-Y"); $sdate = date("Y-m-d"); $refnum = getrefnum(); /*refnum*/ if ($inv['branch'] != 0) { db_conn("cubit"); $get_addr = "SELECT * FROM customer_branches WHERE id = '{$inv['branch']}' LIMIT 1"; $run_addr = db_exec($get_addr); if (pg_numrows($run_addr) < 1) { $address = ""; } else { $barr = pg_fetch_array($run_addr); $address = " - {$barr['branch_name']}"; } } else { $address = ""; } /* --- Updates ---- */ db_connect(); $Sql = "UPDATE invoices SET printed = 'y', done = 'y', invnum='{$invnum}' WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $upRslt = db_exec($Sql) or errDie("Unable to update invoice information"); //dont make consignment order from invoice if customer number is entered ... // if (isset($inv['cordno']) AND strlen($inv['cordno']) > 0){ // $inv_type = "Consignment Order"; // }else { $inv_type = "Invoice"; // } # Record the payment on the statement $sql = "\n\t\t\tINSERT INTO stmnt (\n\t\t\t\tcusnum, invid, docref, amount, date, \n\t\t\t\ttype, branch, div, allocation_date, \n\t\t\t\tallocation_balance\n\t\t\t) VALUES (\n\t\t\t\t'{$inv['cusnum']}', '{$invnum}', '{$inv['docref']}', '{$inv['total']}', '{$inv['odate']}', \n\t\t\t\t'{$inv_type}', '{$address}', '" . USER_DIV . "', '{$inv['odate']}', \n\t\t\t\t'" . abs($inv['total']) . "'\n\t\t\t)"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # Record the payment on the statement $sql = "\n\t\t\tINSERT INTO open_stmnt (\n\t\t\t\tcusnum, invid, docref, amount, balance, \n\t\t\t\tdate, type, div\n\t\t\t) VALUES (\n\t\t\t\t'{$inv['cusnum']}', '{$invnum}', '{$inv['docref']}', '{$inv['total']}','{$inv['total']}', \n\t\t\t\t'{$inv['odate']}', '{$inv_type}', '" . USER_DIV . "'\n\t\t\t)"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # Save invoice discount $sql = "\n\t\t\tINSERT INTO inv_discs (\n\t\t\t\tcusnum, invid, traddisc, itemdisc, inv_date, delchrg, \n\t\t\t\tdiv, total\n\t\t\t) VALUES (\n\t\t\t\t'{$inv['cusnum']}', '{$invnum}', '{$traddiscm}', '{$disc}', '{$inv['odate']}', '{$inv['delchrg']}', \n\t\t\t\t'" . USER_DIV . "', ({$SUBTOT}+{$inv['delchrg']})\n\t\t\t)"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # Update the customer (make balance more) $sql = "UPDATE customers SET balance = (balance + '{$inv['total']}') WHERE cusnum = '{$inv['cusnum']}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); # Make ledge record custledger($inv['cusnum'], $dept['incacc'], $inv['odate'], $invnum, "Invoice No. {$invnum}", $inv['total'], "d"); db_connect(); # get selected stock in this invoice $sql = "SELECT * FROM inv_items WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $tcosamt = 0; $sdate = date("Y-m-d"); $nsp = 0; while ($stkd = pg_fetch_array($stkdRslt)) { $stkd['account'] += 0; if ($stkd['account'] == 0) { db_connect(); # get selamt from selected stock $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); if ($stk['units'] - $stkd['qty'] < 0) { if ($stk['units'] <= 0) { $cosamt = 0; $cosamt2 = 0; } else { $cosamt = round($stk['units'] * $stk['csprice'], 2); $cosamt2 = round($stk['units'] * $stk['csprice'], 4); } } else { $cosamt = round($stkd['qty'] * $stk['csprice'], 2); $cosamt2 = round($stkd['qty'] * $stk['csprice'], 4); } # update stock(alloc - qty) $sql = "\n\t\t\t\t\tUPDATE stock \n\t\t\t\t\tSET csamt = (csamt - '{$cosamt}'),units = (units - '{$stkd['qty']}'), alloc=(alloc - '{$stkd['qty']}') \n\t\t\t\t\tWHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update stock to Cubit.", SELF); if ($inv["pslip_sordid"] > 0) { $sql = "UPDATE stock SET alloc = (alloc - '{$stkd['qty']}') WHERE stkid='{$stkd['stkid']}'"; // db_exec($sql) or errDie("Unable to update allocation."); } ###################VAT CALCS####################### $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($stkd['amt'], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}", $iamount, $ivat); #################################################### $amtexvat = sprint($stkd['amt']); // $uc=sprint($cosamt2/$stkd['qty']); $uc = round($cosamt2 / $stkd['qty'], 4); // '$cosamt', $csprice = sprint($stk['csprice'] * $stkd['qty']); db_connect(); $sql = "\n\t\t\t\t\tINSERT INTO stockrec (\n\t\t\t\t\t\tedate, stkid, stkcod, stkdes, trantype, qty, \n\t\t\t\t\t\tcsprice, csamt, details, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$inv['odate']}', '{$stkd['stkid']}', '{$stk['stkcod']}', '{$stk['stkdes']}', 'invoice', '{$stkd['qty']}', \n\t\t\t\t\t\t'{$amtexvat}', '{$csprice}', 'Stock sold - Invoice No. {$invnum}', '" . USER_DIV . "'\n\t\t\t\t\t)"; $recRslt = db_exec($sql); if ($stk['csprice'] > 0) { $Sl = "INSERT INTO scr (inv, stkid, amount, invid) VALUES ('{$invnum}', '{$stkd['stkid']}', '{$uc}', '{$stkd['id']}')"; $Rg = db_exec($Sl); } if ($stk['serd'] == 'yes') { ext_invSer($stkd['serno'], $stkd['stkid'], "{$inv['cusname']} {$inv['surname']}", $invnum, $inv['odate']); } # stkid, stkcod, stkdes, trantype, edate, qty, csamt, details $sdate = date("Y-m-d"); stockrec($stk['stkid'], $stk['stkcod'], $stk['stkdes'], 'ct', $inv['odate'], $stkd['qty'], $cosamt, "Sold to Customer : {$inv['surname']} - Invoice No. {$invnum}"); # get accounts db_conn("exten"); $sql = "SELECT stkacc, cosacc FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); $stockacc = $wh['stkacc']; $cosacc = $wh['cosacc']; if ($cosamt < 0) { $cosamt = 0; } # dt(cos) ct(stock) writetrans($cosacc, $stockacc, $inv['odate'], $refnum, $cosamt, "Cost Of Sales for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}"); $tcosamt += $cosamt; #record the entry for the stock report db_connect(); $sql = "\n\t\t\t\t\tINSERT INTO salesrec (\n\t\t\t\t\t\tedate, invid, invnum, debtacc, vat, total, typ, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$inv['odate']}', '{$invid}', '{$invnum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'stk', '" . USER_DIV . "'\n\t\t\t\t\t)"; $recRslt = db_exec($sql); } else { db_connect(); ###################VAT CALCS####################### $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($stkd['amt'], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}", $iamount, $ivat); #################################################### $amtexvat = sprint($stkd['amt']); db_connect(); $sdate = date("Y-m-d"); $nsp += sprint($iamount - $ivat); writetrans($dept['debtacc'], $stkd['account'], $inv['odate'], $refnum, $iamount - $ivat, "Debtors Control for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}"); db_connect(); $sql = "\n\t\t\t\t\tINSERT INTO salesrec (\n\t\t\t\t\t\tedate, invid, invnum, debtacc, vat, total, typ, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$inv['odate']}', '{$invid}', '{$invnum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'non', '" . USER_DIV . "'\n\t\t\t\t\t)"; $recRslt = db_exec($sql); } } ###################VAT CALCS####################### $inv['delvat'] += 0; db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$inv['delvat']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { $Sl = "SELECT * FROM vatcodes"; $Ri = db_exec($Sl); } $vd = pg_fetch_array($Ri); if (TAX_VAT != $vd['vat_amount'] and $vd['vat_amount'] != "0.00") { $showvat = FALSE; } if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($inv['delchrg'], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}", sprint($iamount + $ivat), $ivat); #################################################### /* - Start Transactoins - */ # dt(debtors) ct(income/sales) writetrans($dept['debtacc'], $dept['incacc'], $inv['odate'], $refnum, sprint($TOTAL - $VAT - $nsp), "Debtors Control for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}"); # dt(debtors) ct(vat account) writetrans($dept['debtacc'], $vatacc, $inv['odate'], $refnum, $VAT, "VAT Received on Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}"); db_conn('cubit'); $Sl = "\n\t\t\tINSERT INTO sj (\n\t\t\t\tcid, name, des, date, exl, \n\t\t\t\tvat, inc, div\n\t\t\t) VALUES (\n\t\t\t\t'{$inv['cusnum']}', '{$inv['surname']}', 'Invoice {$invnum}', '{$inv['odate']}', '" . sprint($TOTAL - $VAT) . "', \n\t\t\t\t'{$VAT}', '" . sprint($TOTAL) . "', '" . USER_DIV . "'\n\t\t\t)"; $Ri = db_exec($Sl); // db_connect(); // $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div) // VALUES('$inv[odate]', '$invid', '$invnum', '$dept[debtacc]', '$VAT', '$TOTAL', 'stk', '".USER_DIV."')"; // $recRslt = db_exec($sql); //exit; # Commit updates pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF); /* - End Transactoins - */ # vat explanation if ($inv['chrgvat'] == 'nov') { $expl = "VAT Exempt indicator"; } else { $expl = "VAT Exempt indicator"; } if (strlen($inv['comm']) > 0) { $inv['comm'] = "\n\t\t\t<table border='1' cellspacing='0' bordercolor='#000000'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>Remarks:</td>\n\t\t\t\t\t<td>" . nl2br($inv['comm']) . "</td>\n\t\t\t\t</tr>\n\t\t\t</table>"; } $cc = "<script> sCostCenter('dt', 'Sales', '{$inv['odate']}', 'Invoice No.{$invnum} for Customer {$inv['cusname']} {$inv['surname']}', '" . ($TOTAL - $VAT) . "', 'Cost Of Sales for Invoice No.{$invnum}', '{$tcosamt}', ''); </script>"; db_conn('cubit'); $Sl = "SELECT * FROM settings WHERE constant='SALES'"; $Ri = db_exec($Sl) or errDie("Unable to get settings."); $data = pg_fetch_array($Ri); if ($data['value'] == "Yes") { $sp = "\n\t\t\t<tr>\n\t\t\t\t<td><b>Sales Person:</b> {$inv['salespn']}</td>\n\t\t\t</tr>"; } else { $sp = ""; } if ($inv['chrgvat'] == "inc") { $inv['chrgvat'] = "Inclusive"; } elseif ($inv['chrgvat'] == "exc") { $inv['chrgvat'] = "Exclusive"; } else { $inv['chrgvat'] = "No vat"; } if ($inv['branch'] == 0) { $branchname = "Head Office"; } else { $get_bname = "SELECT * FROM customer_branches WHERE id = '{$inv['branch']}' LIMIT 1"; $run_bname = db_exec($get_bname); if (pg_numrows($run_bname) < 1) { $branchname = ""; } else { $arr = pg_fetch_array($run_bname); $branchname = $arr['branch_name']; } } if (!isset($showvat)) { $showvat = TRUE; } if ($showvat == TRUE) { $vat14 = AT14; } else { $vat14 = ""; } if (strlen(COMP_TEL) > 0) { $showtel = "Tel: "; } else { $showtel = ""; } if (strlen(COMP_FAX) > 0) { $showfax = "Fax: "; } else { $showfax = ""; } // Retrieve the company information db_conn("cubit"); $sql = "SELECT * FROM compinfo"; $comp_rslt = db_exec($sql) or errDie("Unable to retrieve company information from Cubit."); $comp_data = pg_fetch_array($comp_rslt); // Retrieve the banking information $bank_data = qryBankAcct(getdSetting("BANK_DET")); // Retrieve customer information db_conn("cubit"); $sql = "SELECT * FROM customers WHERE cusnum='{$inv['cusnum']}'"; $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer information from Cubit."); $cust_data = pg_fetch_array($cust_rslt); $table_borders = "\n\t\tborder-top: 2px solid #000000;\n\t\tborder-left: 2px solid #000000;\n\t\tborder-right: 2px solid #000000;\n\t\tborder-bottom: none;"; $details = ""; for ($i = 0; $i <= $page; $i++) { // new page? if ($i > 1) { $details .= "<br style='page-break-after:always;'>"; } $products_out = ""; foreach ($products[$i] as $string) { $products_out .= $string; } $barcode = "<img src='manufact/" . pick_slip_barcode($inv["invid"], 1) . "' />"; $details .= "\n\t\t<center>\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td>\n\t\t\t<table border='0' cellpadding='2' cellspacing='2' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td align='left'><img src='compinfo/getimg.php' width='230' height='47'>{$barcode}</td>\n\t\t\t\t\t<td align='left'><font size='5'><b>" . COMP_NAME . "</b></font></td>\n\t\t\t\t\t<td align='right'><font size='5'><b>Tax Invoice</b></font></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</td></tr>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td valign='top'>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['addr1']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['paddr1']} </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['addr2']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['paddr2']} </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['addr3']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['paddr3']} </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['addr4']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$comp_data['postcode']} </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>REG:</b> {$comp_data['regnum']}</b> </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>{$bank_data['bankname']}</b> </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>VAT REG:</b> {$comp_data['vatnum']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Branch</b> {$bank_data['branchname']} </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Tel:</b> {$comp_data['tel']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Branch Code:</b> {$bank_data['branchcode']} </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Fax:</b> {$comp_data['fax']} </td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Acc Num:</b> {$bank_data['accnum']} </td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</td><td valign='top'>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Date</b></td>\n\t\t\t\t\t<td><b>Page Number</b></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>{$inv['odate']}</td>\n\t\t\t\t\t<td>" . ($i + 1) . "</td>\n\t\t\t\t</tr>\n\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000; border-right: 2px solid #000'> </td>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000'> </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr><td> </td></tr>\n\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><b>Invoice No:</b> {$invnum}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><b>Proforma Inv No:</b> {$inv['docref']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><b>Sales Order No:</b> {$inv['ordno']}</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td colspan='2'><b>Account No:</b> {$cust_data['accno']}</td>\n\t\t\t\t</tr>\n\t\t\t\t{$sp}\n\t\t\t</table>\n\t\t\t</td></tr>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td align='center'><font size='4'><b>Tax Invoice To:</b></font></td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</td></tr>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td width='33%' style='border-right: 2px solid #000'><b>{$inv['surname']}</b></td>\n\t\t\t\t\t<td width='33%' style='border-right: 2px solid #000'><b>Postal Address</b></td>\n\t\t\t\t\t<td width='33%'><b>Delivery Address</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>" . nl2br($cust_data["addr1"]) . "</td>\n\t\t\t\t\t<td style='border-right: 2px solid #000'>" . nl2br($cust_data["paddr1"]) . "</td>\n\t\t\t\t\t<td>Branch: {$branchname}<br />" . nl2br($inv["del_addr"]) . "</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</td></tr>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td width='33%' style='border-right: 2px solid #000'><b>Customer VAT No:</b> {$inv['cusvatno']}</td>\n\t\t\t\t\t<td width='33%' style='border-right: 2px solid #000'><b>Customer Order No:</b> {$inv['cordno']}</td>\n\t\t\t\t\t<td width='33%'><b>Delivery Date:</b> {$inv['deldate']}</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t\t</td></tr>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000; border-right: 2px solid #000;'><b>Code</b></td>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000; border-right: 2px solid #000;'><b>Description</b></td>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000; border-right: 2px solid #000;'><b>Qty</b></td>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000; border-right: 2px solid #000;' align='right'><b>Unit Price</b></td>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000; border-right: 2px solid #000;' align='right'><b>Unit Discount</b></td>\n\t\t\t\t\t<td style='border-bottom: 2px solid #000' align='right'><b>Amount</b></td>\n\t\t\t\t</tr>\n\t\t\t\t{$products_out}\n\t\t\t</table>\n\t\t\t</td></tr>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='{$table_borders}'>\n\t\t\t<tr><td>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td><i>VAT Exempt Indicator: #</i></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td>{$inv['comm']}</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t</table>\n\n\t\t<table cellpadding='0' cellspacing='0' width='85%' style='border: 2px solid #000000'>\n\t\t\t<tr><td>\n\t\t\t<table cellpadding='2' cellspacing='0' border='0' width='100%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Terms: {$inv['terms']} days</b></td>\n\t\t\t\t\t<td><b>Subtotal:</b></td>\n\t\t\t\t\t<td nowrap><b>" . CUR . " {$inv['subtot']}</b></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t\t\t\t\t<td><b>Trade Discount:</b></td>\n\t\t\t\t\t<td nowrap><b>" . CUR . " {$inv['discount']}</b></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Received in good order by:</b>_____________________</td>\n\t\t\t\t\t<td><b>Delivery Charge</b></td>\n\t\t\t\t\t<td nowrap><b>" . CUR . " {$inv['delivery']}</b></td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'> </td>\n\t\t\t\t\t<td><b>VAT {$vat14}:</b></td>\n\t\t\t\t\t<td nowrap><b>" . CUR . " {$inv['vat']}</b></td>\n\t\t\t\t<tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td style='border-right: 2px solid #000'><b>Date:</b>_____________________</td>\n\t\t\t\t\t<td><b>Total Incl VAT:</b></td>\n\t\t\t\t\t<td nowrap><b>" . CUR . " {$inv['total']}</b></td>\n\t\t\t\t</tr>\n\t\t\t\t</tr>\n\t\t\t</table>\n\t\t</table>"; } // Retrieve template settings from Cubit db_conn("cubit"); $sql = "SELECT filename FROM template_settings WHERE template='invoices'"; $tsRslt = db_exec($sql) or errDie("Unable to retrieve the template settings from Cubit."); $template = pg_fetch_result($tsRslt, 0); $OUTPUT = "\n\t\t<script>\n\t\t\tsCostCenter('dt', 'Sales', '{$inv['odate']}', 'Invoice No.{$invnum} for Customer {$inv['cusname']} {$inv['surname']}', '" . ($TOTAL - $VAT) . "', 'Cost Of Sales for Invoice No.{$invnum}', '{$tcosamt}', '');\n\t\t</script>"; if (isset($email)) { $OUTPUT .= "\n\t\t\t<script>\n\t\t\t\tmove(\"invoices-email.php?evs={$inv['invid']}\");\n\t\t\t</script>"; require "template.php"; } if ($template == "invoice-print.php") { $OUTPUT .= $details; require "tmpl-print.php"; } else { $OUTPUT .= "\n\t\t\t<script>\n\t\t\t\tmove(\"{$template}?invid={$inv['invid']}&type=inv\");\n\t\t\t</script>"; require "template.php"; } }
function details($_GET) { # get vars extract($_GET); # validate input require_lib("validate"); $v = new validate(); $v->isOk($invid, "num", 1, 20, "Invalid invoice number."); # display errors, if any if ($v->isError()) { $err = ""; $errors = $v->getErrors(); foreach ($errors as $e) { $err .= "<li class='err'>" . $e["msg"] . "</li>"; } $confirm .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>"; return $confirm; } # Get invoice info db_connect(); $sql = "SELECT * FROM pinvoices WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $invRslt = db_exec($sql) or errDie("Unable to get invoice information"); if (pg_numrows($invRslt) < 1) { return "<i class='err'>Not Found</i>"; } $inv = pg_fetch_array($invRslt); # CHECK IF THIS DATE IS IN THE BLOCKED RANGE $blocked_date_from = getCSetting("BLOCKED_FROM"); $blocked_date_to = getCSetting("BLOCKED_TO"); if (strtotime($inv['odate']) >= strtotime($blocked_date_from) and strtotime($inv['odate']) <= strtotime($blocked_date_to) and !user_is_admin(USER_ID)) { return "<li class='err'>Period Range Is Blocked. Only an administrator can process entries within this period.</li>"; } if ($inv['rounding'] > 0) { db_conn('core'); $Sl = "SELECT * FROM salesacc WHERE name='rounding'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set the rounding account, under sales settings."; } $ad = pg_fetch_array($Ri); $rac = $ad['accnum']; } if ($inv['cusnum'] != "0") { #then get the actual customer db_connect(); $get_cus = "SELECT * FROM customers WHERE cusnum = '{$inv['cusnum']}' LIMIT 1"; $run_cus = db_exec($get_cus) or errDie("Unable to get customer information"); if (pg_numrows($run_cus) < 1) { #do nothing } else { $carr = pg_fetch_array($run_cus); $inv['cusname'] = "{$carr['cusname']}"; $inv['surname'] = "{$carr['surname']}"; } } $td = $inv['odate']; # check if invoice has been printed if ($inv['printed'] == "y") { $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has already been printed."; $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>"; return $error; } db_conn('cubit'); $sql = "SELECT stkid FROM pinv_items WHERE invid = '{$inv['invid']}' AND div = '" . USER_DIV . "'"; $crslt = db_exec($sql); if (pg_numrows($crslt) < 1) { $error = "<li class='err'> Error : Invoice number <b>{$invid}</b> has no items.</li>"; $error .= "<p><input type='button' onClick='JavaScript:history.back();' value='« Correct submission'>"; return $error; } if ($inv['terms'] == 1) { db_conn('core'); $Sl = "SELECT * FROM salacc WHERE name='cc'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set a link for the POS credit card control account"; } $cd = pg_fetch_array($Ri); $cc = $cd['accnum']; } $change = sprint(sprint($inv['pcash'] + $inv['pcheque'] + $inv['pcc'] + $inv['pcredit']) - sprint($inv['total'] - $inv['rounding'])); $inv['pcash'] = sprint($inv['pcash'] - $change); if ($inv['pcash'] < 0) { $inv['pcash'] = 0; } if (sprint($inv['pcash'] + $inv['pcheque'] + $inv['pcc'] + $inv['pcredit']) != sprint($inv['total'] - $inv['rounding'])) { return "<li class='err'>The total of all the payments is not equal to the invoice total.<br>\n\t\tPlease edit the invoice and try again(You can only overpay with cash)</li>"; } db_connect(); # Begin updates # //lock(2); $invnum = divlastid('inv', USER_DIV); $Sl = "INSERT INTO ncsrec (oldnum,newnum, div) VALUES ('{$invid}','{$invnum}', '" . USER_DIV . "')"; $Rs = db_exec($Sl) or errDie("Unable to insert into db"); //unlock(2); pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF); # get department db_conn("exten"); $sql = "SELECT * FROM departments WHERE deptid = '{$inv['deptid']}' AND div = '" . USER_DIV . "'"; $deptRslt = db_exec($sql); if (pg_numrows($deptRslt) < 1) { $dept['deptname'] = "<i class=err>Not Found</i>"; } else { $dept = pg_fetch_array($deptRslt); } /* --- Start Products Display --- */ # Products layout $products = ""; $disc = 0; # get selected stock in this invoice db_connect(); $sql = "SELECT * FROM pinv_items WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $taxex = 0; $commision = 0; $salesp = qrySalesPersonN($inv["salespn"]); while ($stkd = pg_fetch_array($stkdRslt)) { $stkd['account'] += 0; if ($stkd['account'] == 0) { # get warehouse name db_conn("exten"); $sql = "SELECT whname FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); # get selected stock in this warehouse db_connect(); $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); db_connect(); //this was set to the stock vatcode ??? must be the pur_item code ... $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "<li class='err'>Please select the vatcode for all your stock.</li>"; } $vd = pg_fetch_array($Ri); $sp = " "; # Check Tax Excempt if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $taxex += $stkd['amt']; $ex = "#"; } else { $ex = " "; } # Keep track of discounts $disc += $stkd['disc'] * $stkd['qty']; # Insert stock record $sdate = date("Y-m-d"); $csprice = sprint($stk['csprice'] * $stkd['qty']); # put in product $products .= "\n\t\t\t\t<tr valign='top'>\n\t\t\t\t\t<td>{$stk['stkcod']}</td>\n\t\t\t\t\t<td>{$ex} {$sp} {$stk['stkdes']}</td>\n\t\t\t\t\t<td>{$stkd['qty']}</td>\n\t\t\t\t\t<td>" . sprint($stk["selamt"]) . "</td>\n\t\t\t\t\t<td>" . CUR . sprint($stkd["amt"]) . "</td>\n\t\t\t\t</tr>"; # Get amount exluding vat if including and not exempted $VATP = TAX_VAT; $amtexvat = sprint($stkd['amt']); if ($inv['chrgvat'] == "inc" && $stk['exvat'] != 'yes') { $amtexvat = sprint($stkd['amt'] * 100 / (100 + $VATP)); } $sql = "\n\t\t\t\t\tINSERT INTO stockrec (\n\t\t\t\t\t\tedate, stkid, stkcod, stkdes, trantype, qty, csprice, \n\t\t\t\t\t\tcsamt, details, div\n\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t'{$td}', '{$stkd['stkid']}', '{$stk['stkcod']}', '{$stk['stkdes']}', 'invoice', '{$stkd['qty']}', '{$stkd['amt']}', \n\t\t\t\t\t\t'{$csprice}', 'Stock sold - Invoice No. {$invnum}', '" . USER_DIV . "'\n\t\t\t\t\t)"; $recRslt = db_exec($sql); if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = $stk["com"]; } $commision = $commision + coms($inv['salespn'], $amtexvat, $itemcommission); } else { db_conn('core'); $Sl = "SELECT * FROM accounts WHERE accid='{$stkd['account']}'"; $Ri = db_exec($Sl) or errDie("Unable to get account data."); $ad = pg_fetch_array($Ri); db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); $sp = ""; # Check Tax Excempt if ($vd['zero'] == "Yes") { $taxex += $stkd['amt']; $ex = "#"; } else { $ex = ""; } # all must be excempted if ($inv['chrgvat'] == 'nov') { $ex = "#"; } $VATP = TAX_VAT; $amtexvat = sprint($stkd['amt']); if ($inv['chrgvat'] == "inc") { $amtexvat = sprint($stkd['amt'] * 100 / (100 + $VATP)); } if ($salesp["com"] > 0) { $itemcommission = $salesp['com']; } else { $itemcommission = 0; } $commision = $commision + coms($inv['salespn'], $amtexvat, $itemcommission); # Put in product $products .= "\n\t\t\t\t<tr valign='top'>\n\t\t\t\t\t<td></td>\n\t\t\t\t\t<td>{$ex} {$sp} {$stkd['description']}</td>\n\t\t\t\t\t<td>{$stkd['qty']}</td>\n\t\t\t\t\t<td>" . sprint($stkd["unitcost"]) . "</td>\n\t\t\t\t\t<td>{$stkd['disc']}</td>\n\t\t\t\t\t<td>" . CUR . sprint($stkd["amt"]) . "</td>\n\t\t\t\t</tr>"; } } /* --- Start Some calculations --- */ # subtotal $SUBTOT = sprint($inv['subtot']); # Calculate subtotal $VATP = TAX_VAT; $SUBTOTAL = sprint($inv['subtot']); $VAT = sprint($inv['vat']); $TOTAL = sprint($inv['total']); $av = $VAT; $at = $TOTAL - $VAT; $nt = sprint($inv['pcredit']); $sd = date("Y-m-d"); $ro = $inv['rounding']; $ro += 0; com_invoice($inv['salespn'], $TOTAL - $VAT, $commision, $invnum, $td, true); /* --- End Some calculations --- */ /* - Start Hooks - */ $vatacc = gethook("accnum", "salesacc", "name", "VAT", "novat"); /* - End Hooks - */ $nsp = 0; # todays date $date = date("d-m-Y"); $sdate = date("Y-m-d"); db_conn('cubit'); if ($inv['cusnum'] > 0 && $nt > 0) { # Record the payment on the statement $sql = "\n\t\t\t\tINSERT INTO stmnt \n\t\t\t\t\t(cusnum, invid, docref, amount, date, type, div, allocation_date) \n\t\t\t\tVALUES \n\t\t\t\t\t('{$inv['cusnum']}', '{$invnum}', '0', '{$nt}', '{$inv['odate']}', 'Invoice', '" . USER_DIV . "', '{$inv['odate']}')"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # Record the payment on the statement $sql = "INSERT INTO open_stmnt(cusnum, invid, docref, amount, balance, date, type, div) VALUES('{$inv['cusnum']}', '{$invnum}', '0', '{$nt}', '{$nt}', '{$inv['odate']}', 'Invoice', '" . USER_DIV . "')"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # Update the customer (make balance more) $sql = "UPDATE customers SET balance = (balance + '{$nt}') WHERE cusnum = '{$inv['cusnum']}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update invoice in Cubit.", SELF); custledger($inv['cusnum'], $dept['incacc'], $inv['odate'], $invnum, "Invoice No. {$invnum}", $nt, "d"); recordDT($nt, $inv['cusnum'], $inv['odate']); db_conn('cubit'); $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$nt}','Credit','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } db_conn('cubit'); if ($inv['terms'] == 1) { $Sl = "INSERT INTO crec(userid,username,amount,pdate,inv) VALUES ('" . USER_ID . "','" . USER_NAME . "','{$TOTAL}','{$td}','{$invnum}')"; $Ry = db_exec($Sl) or errDie("Unable to insert pos record."); } else { $Sl = "INSERT INTO posrec(userid,username,amount,pdate,inv) VALUES ('" . USER_ID . "','" . USER_NAME . "','{$TOTAL}','{$td}','{$invnum}')"; $Ry = db_exec($Sl) or errDie("Unable to insert pos record."); } if (!isset($inv['cusname']) or strlen($inv['cusname']) < 1) { $custname = $inv['surname']; } else { $custname = $inv['cusname']; } $Sl = "INSERT INTO pr(userid,username,amount,pdate,inv,cust,t) VALUES ('" . USER_ID . "','" . USER_NAME . "','{$TOTAL}','{$td}','{$invnum}','{$custname}','{$inv['terms']}')"; $Ry = db_exec($Sl) or errDie("Unable to insert pos record."); $refnum = getrefnum(); /*refnum*/ $fcash = $inv['pcash']; $fccp = $inv['pcc']; $fcheque = $inv['pcheque']; $fcredit = $inv['pcredit']; /* --- Updates ---- */ db_connect(); $Sql = "UPDATE pinvoices SET pchange='{$change}',printed ='y', done ='y',invnum='{$invnum}' WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $upRslt = db_exec($Sql) or errDie("Unable to update invoice information"); # save invoice discount $sql = "INSERT INTO inv_discs(cusnum, invid, traddisc, itemdisc, inv_date, delchrg, div,total) VALUES('0','{$invnum}','{$inv['delivery']}','{$disc}', '{$inv['odate']}', '{$inv['delivery']}', '" . USER_DIV . "',({$SUBTOT}+{$inv['delivery']}))"; $stmntRslt = db_exec($sql) or errDie("Unable to insert statement record in Cubit.", SELF); # get selected stock in this invoice $sql = "SELECT * FROM pinv_items WHERE invid = '{$invid}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); $tcosamt = 0; while ($stkd = pg_fetch_array($stkdRslt)) { $stkd['account'] += 0; if ($stkd['account'] == 0) { db_connect(); # get selamt from selected stock $sql = "SELECT * FROM stock WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'"; $stkRslt = db_exec($sql); $stk = pg_fetch_array($stkRslt); if ($stk['units'] - $stkd['qty'] < 0) { if ($stk['units'] < 0) { $stk['units'] = 0; } $cosamt = round($stk['units'] * $stk['csprice'], 2); } else { $cosamt = round($stkd['qty'] * $stk['csprice'], 2); } # cost amount //$cosamt = round(($stkd['qty'] * $stk['csprice']), 2); if ($stk['csprice'] > 0) { $Sl = "INSERT INTO scr(inv,stkid,amount) VALUES ('{$invnum}','{$stkd['stkid']}',' {$stk['csprice']}')"; $Rg = db_exec($Sl); } # update stock(alloc - qty) $sql = "UPDATE stock SET csamt = (csamt - '{$cosamt}'),units = (units - '{$stkd['qty']}'),alloc = (alloc - '{$stkd['qty']}') WHERE stkid = '{$stkd['stkid']}' AND div = '" . USER_DIV . "'"; $rslt = db_exec($sql) or errDie("Unable to update stock to Cubit.", SELF); $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vat code for all your stock."; } $VATP = TAX_VAT; $amtexvat = sprint($stkd['amt']); ###################VAT CALCS####################### $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($stk['exvat'] == 'yes' || $vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($stkd['amt'], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}", $iamount, $ivat); #################################################### if ($stk['serd'] == 'yes') { ext_invSer($stkd['serno'], $stkd['stkid'], "POS Cash", $invnum); } # stkid, stkcod, stkdes, trantype, edate, qty, csamt, details $sdate = date("Y-m-d"); stockrec($stk['stkid'], $stk['stkcod'], $stk['stkdes'], 'ct', $td, $stkd['qty'], $cosamt, "POS Sales - Invoice No. {$invnum}"); # get accounts db_conn("exten"); $sql = "SELECT stkacc,cosacc FROM warehouses WHERE whid = '{$stkd['whid']}' AND div = '" . USER_DIV . "'"; $whRslt = db_exec($sql); $wh = pg_fetch_array($whRslt); $stockacc = $wh['stkacc']; $cosacc = $wh['cosacc']; # dt(cos) ct(stock) writetrans($cosacc, $stockacc, $td, $refnum, $cosamt, "Cost Of Sales POS Cash on POS Invoice No.{$invnum}."); $tcosamt += $cosamt; db_connect(); $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div)\n\t\t\t\tVALUES('{$inv['odate']}', '{$invid}', '{$invnum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'stk', '" . USER_DIV . "')"; $recRslt = db_exec($sql); } else { db_connect(); ###################VAT CALCS####################### $Sl = "SELECT * FROM vatcodes WHERE id='{$stkd['vatcode']}'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please select the vatcode for all your stock."; } $vd = pg_fetch_array($Ri); if ($vd['zero'] == "Yes") { $excluding = "y"; } else { $excluding = ""; } $vr = vatcalc($stkd['amt'], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; $av -= $ivat; $at -= $iamount; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}", $iamount, $ivat); db_connect(); $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div)\n\t\t\t\tVALUES('{$inv['odate']}', '{$invid}', '{$invnum}', '{$dept['debtacc']}', '{$ivat}', '{$iamount}', 'non', '" . USER_DIV . "')"; $recRslt = db_exec($sql); #################################################### $amtexvat = sprint($stkd['amt']); db_connect(); $sdate = date("Y-m-d"); $nsp += sprint($iamount - $ivat); // //writetrans($cosacc, $stockacc,$inv['odate'] , $refnum, $cosamt, "Cost Of Sales for Invoice No.$invnum for Customer : $inv[cusname] $inv[surname]"); // writetrans($dept['debtacc'], $stkd['account'],$inv['odate'], $refnum, ($iamount-$ivat), "Debtors Control for Invoice No.$invnum for Customer : $inv[cusname] $inv[surname]"); if ($inv['pcash'] > 0) { $min = $ro; $inv['pcash'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcash'] >= $ivat) { writetrans($dept['pca'], $vatacc, $td, $refnum, $ivat, "VAT Received for POS Invoice No.{$invnum}."); $inv['pcash'] = sprint($inv['pcash'] - $ivat); $ivat = 0; if ($inv['pcash'] > 0) { if ($inv['pcash'] >= $iamount) { writetrans($dept['pca'], $stkd['account'], $td, $refnum, $iamount, "Sales for POS Invoice No.{$invnum}."); $inv['pcash'] = sprint($inv['pcash'] - $iamount); $iamount = 0; } elseif ($inv['pcash'] < $iamount) { writetrans($dept['pca'], $stkd['account'], $td, $refnum, $inv['pcash'], "Sales for POS Invoice No.{$invnum}."); $iamount = sprint($iamount - $inv['pcash']); $inv['pcash'] = 0; } } } else { writetrans($dept['pca'], $vatacc, $td, $refnum, $inv['pcash'], "VAT Received for POS Invoice No.{$invnum}."); $ivat = sprint($ivat - $inv['pcash']); $inv['pcash'] = 0; } // db_conn('cubit'); // // $inv['pcash']-=$min; // // $Sl="INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('$sd','".USER_NAME."','$invnum','$inv[pcash]','Cash','".PRD_DB."','0')"; // $Ri=db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcheque'] > 0) { $min = $ro; $inv['pcheque'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcheque'] >= $ivat) { writetrans($dept['pca'], $vatacc, $td, $refnum, $ivat, "VAT Received for POS Invoice No.{$invnum}."); $inv['pcheque'] = sprint($inv['pcheque'] - $ivat); $ivat = 0; if ($inv['pcheque'] > 0) { if ($inv['pcheque'] >= $iamount) { writetrans($dept['pca'], $stkd['account'], $td, $refnum, $iamount, "Sales for POS Invoice No.{$invnum}."); $inv['pcheque'] = sprint($inv['pcheque'] - $iamount); $iamount = 0; } elseif ($inv['pcheque'] < $iamount) { writetrans($dept['pca'], $stkd['account'], $td, $refnum, $inv['pcheque'], "Sales for POS Invoice No.{$invnum}."); $iamount = sprint($iamount - $inv['pcheque']); $inv['pcheque'] = 0; } } } else { writetrans($dept['pca'], $vatacc, $td, $refnum, $inv['pcheque'], "VAT Received for POS Invoice No.{$invnum}."); $ivat = sprint($ivat - $inv['pcheque']); $inv['pcheque'] = 0; } // db_conn('cubit'); // // $inv['pcash']-=$min; // // $Sl="INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('$sd','".USER_NAME."','$invnum','$inv[pcash]','Cash','".PRD_DB."','0')"; // $Ri=db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcc'] > 0) { db_conn('core'); $Sl = "SELECT * FROM salacc WHERE name='cc'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set a link for the POS credit card control account"; } $cd = pg_fetch_array($Ri); $cc = $cd['accnum']; $min = $ro; $inv['pcc'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcc'] >= $ivat) { writetrans($cc, $vatacc, $td, $refnum, $ivat, "VAT Received for POS Invoice No.{$invnum}."); $inv['pcc'] = sprint($inv['pcc'] - $ivat); $ivat = 0; if ($inv['pcc'] > 0) { if ($inv['pcc'] >= $iamount) { writetrans($cc, $stkd['account'], $td, $refnum, $iamount, "Sales for POS Invoice No.{$invnum}."); $inv['pcc'] = sprint($inv['pcc'] - $iamount); $iamount = 0; } elseif ($inv['pcc'] < $iamount) { writetrans($cc, $stkd['account'], $td, $refnum, $inv['pcc'], "Sales for POS Invoice No.{$invnum}."); $iamount = sprint($iamount - $inv['pcc']); $inv['pcc'] = 0; } } } else { writetrans($cc, $vatacc, $td, $refnum, $inv['pcc'], "VAT Received for POS Invoice No.{$invnum}."); $ivat = sprint($ivat - $inv['pcc']); $inv['pcc'] = 0; } // db_conn('cubit'); // // $inv['pcash']-=$min; // // $Sl="INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('$sd','".USER_NAME."','$invnum','$inv[pcash]','Cash','".PRD_DB."','0')"; // $Ri=db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcredit'] > 0) { db_conn('core'); $min = $ro; $inv['pcredit'] += $ro; $ro = 0; //$amount=$inv['pcash']; if ($inv['pcredit'] >= $ivat) { writetrans($dept['debtacc'], $vatacc, $td, $refnum, $ivat, "VAT Received for POS Invoice No.{$invnum}."); $inv['pcredit'] = sprint($inv['pcredit'] - $ivat); $ivat = 0; if ($inv['pcredit'] > 0) { if ($inv['pcredit'] >= $iamount) { writetrans($dept['debtacc'], $stkd['account'], $td, $refnum, $iamount, "Sales for POS Invoice No.{$invnum}."); $inv['pcredit'] = sprint($inv['pcredit'] - $iamount); $iamount = 0; } elseif ($inv['pcredit'] < $iamount) { writetrans($dept['debtacc'], $stkd['account'], $td, $refnum, $inv['pcredit'], "Sales for POS Invoice No.{$invnum}."); $iamount = sprint($iamount - $inv['pcredit']); $inv['pcredit'] = 0; } } } else { writetrans($dept['debtacc'], $vatacc, $td, $refnum, $inv['pcredit'], "VAT Received for POS Invoice No.{$invnum}."); $ivat = sprint($ivat - $inv['pcredit']); $inv['pcredit'] = 0; } // db_conn('cubit'); // // $inv['pcash']-=$min; // // $Sl="INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('$sd','".USER_NAME."','$invnum','$inv[pcash]','Cash','".PRD_DB."','0')"; // $Ri=db_exec($Sl) or errDie("Unable to insert data."); } } } /* - Start Transactoins - */ ###################VAT CALCS####################### db_conn('cubit'); $Sl = "SELECT * FROM vatcodes WHERE del='Yes'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { $Sl = "SELECT * FROM vatcodes"; $Ri = db_exec($Sl); } $vd = pg_fetch_array($Ri); $excluding = ""; $vr = vatcalc($inv['delchrg'], $inv['chrgvat'], $excluding, $inv['traddisc'], $vd['vat_amount']); $vrs = explode("|", $vr); $ivat = $vrs[0]; $iamount = $vrs[1]; vatr($vd['id'], $inv['odate'], "OUTPUT", $vd['code'], $refnum, "VAT for Invoice No.{$invnum} for Customer : {$inv['cusname']} {$inv['surname']}", $iamount, $ivat); #################################################### //print $inv['pcash'];exit; if ($inv['pcash'] > 0) { $min = $ro; $inv['pcash'] += $ro; $ro = 0; $amount = $inv['pcash']; if ($amount >= $av) { writetrans($dept['pca'], $vatacc, $td, $refnum, $av, "VAT Received for POS Invoice No.{$invnum}."); // PROBLEM HERE? $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['pca'], $dept['pia'], $td, $refnum, $amount, "Sales for POS Invoice No.{$invnum}."); $at = $at - $amount; } } else { writetrans($dept['pca'], $vatacc, $td, $refnum, $amount, "VAT Received for POS Invoice No.{$invnum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcash'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fcash}','Cash','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); $fcash = 0; } db_conn('cubit'); if ($fcash > 0) { $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fcash}','Cash','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcheque'] > 0) { $min = $ro; $inv['pcheque'] += $ro; $ro = 0; $amount = $inv['pcheque']; if ($amount >= $av) { writetrans($dept['pca'], $vatacc, $td, $refnum, $av, "VAT Received for POS Invoice No.{$invnum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['pca'], $dept['pia'], $td, $refnum, $amount, "Sales for POS Invoice No.{$invnum}."); $at = $at - $amount; } } else { writetrans($dept['pca'], $vatacc, $td, $refnum, $amount, "VAT Received for POS Invoice No.{$invnum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcheque'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fcheque}','Cheque','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); $fcheque = 0; } db_conn('cubit'); if ($fcheque > 0) { $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fcheque}','Cheque','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcc'] > 0) { db_conn('core'); $Sl = "SELECT * FROM salacc WHERE name='cc'"; $Ri = db_exec($Sl); if (pg_num_rows($Ri) < 1) { return "Please set a link for the POS credit card control account"; } $cd = pg_fetch_array($Ri); $cc = $cd['accnum']; $min = $ro; $inv['pcc'] += $ro; $ro = 0; $amount = $inv['pcc']; if ($amount >= $av) { writetrans($cc, $vatacc, $td, $refnum, $av, "VAT Received for POS Invoice No.{$invnum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($cc, $dept['pia'], $td, $refnum, $amount, "Sales for POS Invoice No.{$invnum}."); $at = $at - $amount; } } else { writetrans($cc, $vatacc, $td, $refnum, $amount, "VAT Received for POS Invoice No.{$invnum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcc'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fccp}','Credit Card','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); $fccp = 0; } db_conn('cubit'); if ($fccp > 0) { $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fccp}','Credit Card','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['pcredit'] > 0) { db_conn('core'); $min = $ro; $inv['pcredit'] += $ro; $ro = 0; $amount = $inv['pcredit']; if ($amount >= $av) { writetrans($dept['debtacc'], $vatacc, $td, $refnum, $av, "VAT Received for POS Invoice No.{$invnum}."); $amount = sprint($amount - $av); $av = 0; if ($amount > 0) { writetrans($dept['debtacc'], $dept['pia'], $td, $refnum, $amount, "Sales for POS Invoice No.{$invnum}."); $at = $at - $amount; } } else { writetrans($dept['debtacc'], $vatacc, $td, $refnum, $amount, "VAT Received for POS Invoice No.{$invnum}."); $av = $av - $amount; $amount = 0; } db_conn('cubit'); $inv['pcc'] -= $min; $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fcredit}','Credit','" . PRD_DB . "','0')"; //$Ri=db_exec($Sl) or errDie("Unable to insert data."); $fcredit = 0; } db_conn('cubit'); if ($fcredit > 0) { $Sl = "INSERT INTO payrec(date,by,inv,amount,method,prd,note) VALUES ('{$td}','" . USER_NAME . "','{$invnum}','{$fcredit}','Credit','" . PRD_DB . "','0')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } if ($inv['rounding'] > 0) { if ($inv['pcash'] > 0) { writetrans($rac, $dept['pca'], $td, $refnum, $inv['rounding'], "Rounding on Invoice No.{$invnum}."); } elseif ($inv['pcheque'] > 0) { writetrans($rac, $dept['pca'], $td, $refnum, $inv['rounding'], "Rounding on Invoice No.{$invnum}."); } elseif ($inv['pcc'] > 0) { writetrans($rac, $cc, $td, $refnum, $inv['rounding'], "Rounding on Invoice No.{$invnum}."); } elseif ($inv['pcredit'] > 0) { writetrans($rac, $dept['debtacc'], $td, $refnum, $inv['rounding'], "Rounding on Invoice No.{$invnum}."); } } // if($inv['terms']==1) { // $dept['pca']=$cc; // } // // # dt(debtors) ct(income/sales) // writetrans($dept['pca'], $dept['pia'], $td, $refnum, ($TOTAL-$VAT), "Sales for POS Invoice No.$invnum."); // // # dt(debtors) ct(vat account) // writetrans($dept['pca'], $vatacc, $td, $refnum, $VAT, "VAT Received for POS Invoice No.$invnum."); // db_connect(); // $sql = "INSERT INTO salesrec(edate, invid, invnum, debtacc, vat, total, typ, div) // VALUES('$inv[odate]', '$invid', '$invnum', '$dept[debtacc]', '$VAT', '$TOTAL', 'stk', '".USER_DIV."')"; // $recRslt = db_exec($sql); db_conn('cubit'); if ($inv['cusnum'] > 0) { db_conn('cubit'); $Sl = "SELECT * FROM customers WHERE cusnum='{$inv['cusnum']}'"; $Ri = db_exec($Sl) or errDie("Unable to get data."); $cd = pg_fetch_array($Ri); $inv['cusname'] = $cd['surname']; } $Sl = "INSERT INTO sj(cid,name,des,date,exl,vat,inc,div) VALUES\n\t('{$inv['cusnum']}','{$inv['cusname']}','POS Invoice {$invnum}','{$inv['odate']}','" . sprint($TOTAL - $VAT) . "','{$VAT}','" . sprint($TOTAL) . "','" . USER_DIV . "')"; $Ri = db_exec($Sl); if ($change > 0) { $Sl = "INSERT INTO pc(date,by,inv,amount) VALUES ('{$sd}','" . USER_NAME . "','{$invnum}','{$change}')"; $Ri = db_exec($Sl) or errDie("Unable to insert data."); } db_conn('cubit'); if ($inv['rounding'] > 0) { $Sl = "INSERT INTO varrec(inv,date,amount) VALUES('{$invnum}','" . date("Y-m-d") . "','{$inv['rounding']}')"; $Ri = db_exec($Sl); } # Commit updates pglib_transaction("COMMIT") or errDie("Unable to commit a database transaction.", SELF); if (strlen($inv['comm']) > 0) { $Com = "<table><tr><td>" . nl2br($inv['comm']) . "</td></tr></table>"; } else { $Com = ""; } $cc = "<script> sCostCenter('dt', 'Sales', '{$inv['odate']}', 'POS Invoice No.{$invnum}', '" . ($TOTAL - $VAT) . "', 'Cost Of Sales for Invoice No.{$invnum}', '{$tcosamt}', ''); </script>"; if ($inv['chrgvat'] == "inc") { $inv['chrgvat'] = "Inclusive"; } elseif ($inv['chrgvat'] == "exc") { $inv['chrgvat'] = "Exclusive"; } else { $inv['chrgvat'] = "No vat"; } /* - End Transactoins - */ /* -- Final Layout -- */ $details = "\n\t\t\t\t\t<center>\n\t\t\t\t\t{$cc}\n\t\t\t\t\t<h2>Tax Invoice</h2>\n\t\t\t\t\t<table cellpadding='0' cellspacing='1' border=0 width=750>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td valign='top' width='40%'>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t\t\t\t<tr><td>{$inv['surname']}</td></tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='top' width='35%'>\n\t\t\t\t\t\t\t\t" . COMP_NAME . "<br>\n\t\t\t\t\t\t\t\t" . COMP_ADDRESS . "<br>\n\t\t\t\t\t\t\t\t" . COMP_TEL . "<br>\n\t\t\t\t\t\t\t\t" . COMP_FAX . "<br>\n\t\t\t\t\t\t\t\tReg No. " . COMP_REGNO . "<br>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td valign='bottom' align='right' width='25%'>\n\t\t\t\t\t\t\t\t<table cellpadding='2' cellspacing='0' border='1' bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Invoice No.</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$invnum}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Order No.</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$inv['ordno']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Terms</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>Cash</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Invoice Date</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$inv['odate']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>VAT</b></td>\n\t\t\t\t\t\t\t\t\t\t<td valign='center'>{$inv['chrgvat']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='3'>\n\t\t\t\t\t\t\t\t<table cellpadding='5' cellspacing='0' border='1' width=100% bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th>ITEM NUMBER</th>\n\t\t\t\t\t\t\t\t\t\t<th width='45%'>DESCRIPTION</th>\n\t\t\t\t\t\t\t\t\t\t<th>QTY</th>\n\t\t\t\t\t\t\t\t\t\t<th>UNIT PRICE</th>\n\t\t\t\t\t\t\t\t\t\t<th>AMOUNT</th>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t{$products}\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>{$Com}</td>\n\t\t\t\t\t\t\t<td align='right' colspan='2'>\n\t\t\t\t\t\t\t\t<table cellpadding='5' cellspacing='0' border='1' width='50%' bordercolor='#000000'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>SUBTOTAL</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$SUBTOT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Trade Discount</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['discount']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>Delivery Charge</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$inv['delivery']}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td><b>VAT @ {$VATP}%</b></td>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$VAT}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th><b>GRAND TOTAL<b></th>\n\t\t\t\t\t\t\t\t\t\t<td align='right'>" . CUR . " {$TOTAL}</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr><td><br></td></tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t<table " . TMPL_tblDflts . " border='1'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td colspan='2'>VAT Exempt indicator = #</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<th>VAT No.</th>\n\t\t\t\t\t\t\t\t\t\t<td align='center'>" . COMP_VATNO . "</td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t </table>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td><br></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t</center>"; /* Start moving invoices */ db_connect(); # Move invoices that are fully paid $sql = "SELECT * FROM pinvoices WHERE printed = 'y' AND done = 'y' AND div = '" . USER_DIV . "'"; $invbRslt = db_exec($sql) or errDie("Unable to update Invoice information in Cubit.", SELF); while ($invb = pg_fetch_array($invbRslt)) { db_conn($invb['prd']); $invb['invnum'] += 0; # Insert invoice to period DB $sql = "INSERT INTO pinvoices(invid,invnum, deptid, cusnum, deptname, cusacc, cusname, telno,\n\t\t\t\t\tsurname, cusaddr, cusvatno, cordno, ordno, chrgvat, terms, traddisc, salespn,\n\t\t\t\t\todate, delchrg, subtot, vat, total, balance, comm, printed, done, div, username,\n\t\t\t\t\trounding,delvat,vatnum,pcash,pcheque,pcc,pcredit, pslip_sordid)\n\t\t\t\tVALUES('{$invb['invid']}','{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}',\n\t\t\t\t\t'{$invb['deptname']}', '{$invb['cusacc']}', '{$invb['cusname']}', '{$invb['telno']}', '{$invb['surname']}',\n\t\t\t\t\t'{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', '{$invb['ordno']}',\n\t\t\t\t\t'{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}',\n\t\t\t\t\t'{$invb['odate']}', '{$invb['delchrg']}', '{$invb['subtot']}', '{$invb['vat']}' ,\n\t\t\t\t\t'{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', 'y', 'y', '" . USER_DIV . "',\n\t\t\t\t\t'" . USER_NAME . "','{$invb['rounding']}','{$invb['delvat']}','{$invb['vatnum']}',\n\t\t\t\t\t'{$invb['pcash']}','{$invb['pcheque']}','{$invb['pcc']}','{$invb['pcredit']}', '{$invb['pslip_sordid']}')"; $rslt = db_exec($sql) or errDie("Unable to insert invoice to the period database.", SELF); db_connect(); $sql = "INSERT INTO movinv(invtype, invnum, prd, docref, div) VALUES('pos', '{$invb['invnum']}', '{$invb['prd']}', '', '" . USER_DIV . "')"; $rslt = db_exec($sql) or errDie("Unable to insert invoice to the period database.", SELF); # get selected stock in this invoice db_connect(); $sql = "SELECT * FROM pinv_items WHERE invid = '{$invb['invid']}' AND div = '" . USER_DIV . "'"; $stkdRslt = db_exec($sql); while ($stkd = pg_fetch_array($stkdRslt)) { db_conn($invb['prd']); # insert invoice items $stkd['vatcode'] += 0; $stkd['account'] += 0; $sql = "INSERT INTO pinv_items(invid, whid, stkid, qty, unitcost, amt, disc, discp, serno, div,vatcode,account,description) VALUES('{$invb['invid']}', '{$stkd['whid']}', '{$stkd['stkid']}', '{$stkd['qty']}', '{$stkd['unitcost']}', '{$stkd['amt']}', '{$stkd['disc']}', '{$stkd['discp']}', '{$stkd['serno']}', '" . USER_DIV . "','{$stkd['vatcode']}','{$stkd['account']}','{$stkd['description']}')"; $rslt = db_exec($sql) or errDie("Unable to insert invoice items to Cubit.", SELF); } db_connect(); # Remove those invoices from running DB $sql = "DELETE FROM pinvoices WHERE invid = '{$invb['invid']}' AND div = '" . USER_DIV . "'"; $delRslt = db_exec($sql) or errDie("Unable to update Invoice information in Cubit.", SELF); # Remove those invoice items from running DB $sql = "DELETE FROM pinv_items WHERE invid = '{$invb['invid']}' AND div = '" . USER_DIV . "'"; $delRslt = db_exec($sql) or errDie("Unable to update Invoice information in Cubit.", SELF); } /* end moving invoices */ /* OLD $OUTPUT = $details; require("tmpl-print.php");*/ header("Location: pos-slip.php?invid={$inv['invid']}&prd={$inv['prd']}&cccc=yes"); exit; }