function update($_POST) { extract($_POST); $collect_ar = array(); if (!empty($client_collect)) { $collect_ar[] = "Client Collect"; } if (!empty($collect)) { $collect_ar[] = "Collect"; } if (!empty($deliver)) { $collect_ar[] = "Deliver"; } $collection = implode(", ", $collect_ar); // if ((in_array("Collect", $collect_ar) && in_array("Client Collect", $collect_ar)) // || (count($collect_ar) == 3)) { // return "<li class='err'>Invalid collection options selected.</li>"; // } if (count($collect_ar) > 1 && in_array("Client Collect", $collect_ar)) { return "<li class='err'>Invalid collection options selected.</li>"; } $temp_assets = explode(",", $temp_assets); pglib_transaction("BEGIN"); if (isset($nhalf_day) && $nhalf_day == "checked") { $nhalf_day = 1; } else { $nhalf_day = 0; } if (isset($nweekends) && $nweekends == "checked") { $nweekends = 1; } else { $nweekends = 0; } $sql = "UPDATE hire.hire_invoices SET comm='{$comm}' WHERE invid='{$invid}'"; $comm_rslt = db_exec($sql) or errDie("Unable to retrieve invoice."); foreach ($temp_assets as $key => $value) { $sql = "SELECT * FROM cubit.assets WHERE id='{$key}'"; $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset."); $asset_data = pg_fetch_array($asset_rslt); } if (isset($amount)) { foreach ($amount as $key => $value) { // if (empty($monthly)) { // $amount[$key] = ""; // } if (!user_is_admin(USER_ID)) { $amount[$key] = ""; } if (!isset($half_day[$key]) || empty($half_day[$key])) { $half_day[$key] = 0; } if (!isset($weekends[$key]) || empty($weekends[$key])) { $weekends[$key] = 0; } else { $weekends[$key] = 1; } if (empty($amount[$key]) && $amount != "0") { if ($basis[$key] == "per_day") { $hifrm = "{$mfrm_year[$key]}-{$mfrm_month[$key]}-{$mfrm_day[$key]}"; $hito = "{$mto_year[$key]}-{$mto_month[$key]}-{$mto_day[$key]}"; $hours = "0"; /* calculate amount */ $ftime = getDTEpoch("{$hifrm} 0:00:00"); $ttime = getDTEpoch("{$hito} 0:00:00"); $days = 0; $weeks = 0; while ($ftime <= $ttime) { if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) { $days += 0.6; } else { ++$days; } $ftime += 24 * 60 * 60; } if (is_numeric($rain_days[$key])) { $days -= $rain_days[$key]; } $timeunits = $days; } else { if ($basis[$key] == "per_hour") { $hifrm = $hito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $hours; $weeks = 0; if (empty($hours) || !is_numeric($hours)) { return "\r\n\t\t\t\t\t\t\t<li class='err'>\r\n\t\t\t\t\t\t\t\t<b>ERROR</b>: Invalid amount of hours.\r\n\t\t\t\t\t\t\t</li>"; } } else { if ($nbasis == "per_week") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $weeks; $hours = 0; if (empty($weeks) || !is_numeric($weeks)) { return "\r\n\t\t\t\t\t\t\t<li class='err'>\r\n\t\t\t\t\t\t\t\t<b>ERROR</b>: Invalid amount of weeks.\r\n\t\t\t\t\t\t\t</li>"; } } } } if ($half_day[$key]) { $amount[$key] = $qty[$key] * $timeunits * (basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key]) - basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key] + basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key] / 2; } else { $amount[$key] = $qty[$key] * $timeunits * basisPrice($cusnum, $asset_id[$key], $basis[$key]); } } if ($amount[$key] == 0) { $amount[$key] = 0; $blank_amount = 1; } else { $blank_amount = 0; } $sql = "UPDATE hire.hire_invitems SET amt='{$amount[$key]}',\r\n\t\t\t\t\t\thalf_day='{$half_day[$key]}', weekends='{$weekends[$key]}'\r\n\t\t\t\t\tWHERE id='{$key}'"; db_exec($sql) or errDie("Unable to update item amount."); $sql = "UPDATE hire.reprint_invitems SET amt='{$amount[$key]}',\r\n\t\t\t\t\t\thalf_day='{$half_day[$key]}', weekends='{$weekends[$key]}'\r\n\t\t\t\t\tWHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to update return item amount."); if ($blank_amount) { $amount[$key] = ""; } //$hifrm = "$hifrm_year[$key]-$hifrm_month[$key]-$hifrm_day[$key]"; //$hito = "$hito_year[$key]-$hito_month[$key]-$hito_day[$key]"; if (!isset($remove[$key])) { $sql = "SELECT basis FROM hire.hire_invitems WHERE id='{$key}'"; $item_rslt = db_exec($sql) or errDie("Unable to retrieve basis."); $mbasis = pg_fetch_result($item_rslt, 0); /* determine time units */ if ($mbasis == "per_day") { $mfrm = mkdate($mfrm_year[$key], $mfrm_month[$key], $mfrm_day[$key]); $mto = mkdate($mto_year[$key], $mto_month[$key], $mto_day[$key]); /* calculate amount */ $ftime = mktime(0, 0, 0, $mfrm_month[$key], $mfrm_day[$key], $mfrm_year[$key]); $ttime = mktime(0, 0, 0, $mto_month[$key], $mto_day[$key], $mto_year[$key]); $days = 0; if (empty($weeks)) { $weeks = 0; } if (empty($hours)) { $hours = 0; } while ($ftime <= $ttime) { if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) { $days += 0.6; } else { ++$days; } $ftime += 24 * 60 * 60; } $timeunits = $days; $sql = "UPDATE hire.hire_invitems\r\n\t\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}'\r\n\t\t\t\t\t\t\tWHERE id='{$key}'"; db_exec($sql) or errDie("Unable to update items."); $sql = "UPDATE hire.reprint_invitems\r\n\t\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}'\r\n\t\t\t\t\t\t\tWHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to update reprint items."); } } else { // Delete the old items $sql = "DELETE FROM hire.hire_invitems WHERE id='{$key}'"; db_exec($sql) or errDie("Unable to remove old items."); $sql = "DELETE FROM hire.reprint_invitems WHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to remove old reprint items."); //.Remove if the item has been hired as well $sql = "DELETE FROM hire.assets_hired WHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to remove items from hired log."); } } } $sql = "SELECT * FROM hire.hire_invoices WHERE invid='{$invid}'"; $hi_rslt = db_exec($sql) or errDie("Unable to retrieve invoice."); $invb = pg_fetch_array($hi_rslt); // Insert new items if ($nasset_id != "0" || $nbasis != "0") { if ($nasset_id == "0") { return "<li class='err'><b>ERROR</b>: No asset selected.</li>"; } /* get asset id */ list($serialqty, $nasset_id) = explode(":", $nasset_id); /* disabled items don't get passed through */ if ($serialqty == "s" || !isset($nqty)) { $nqty = "1"; } else { $sql = "SELECT serial2 FROM cubit.assets WHERE id='{$nasset_id}'"; $dqty_rslt = db_exec($sql) or errDie("Unable to retrieve assets."); $dqty = pg_fetch_result($dqty_rslt, 0); if ($dqty - $nqty < 0) { return "<li class='err'><b>ERROR</b>: Invalid quantity. Only <b>{$dqty}</b> available.</li>"; } } if (empty($nqty) || !is_numeric($nqty)) { return "<li class='err'><b>ERROR</b>: Invalid quantity</li>"; } /* determine time units */ if ($nbasis == "per_day") { $nhifrm = mkdate($nhifrm_year, $nhifrm_month, $nhifrm_day); $nhito = mkdate($nhito_year, $nhito_month, $nhito_day); $hours = "0"; /* calculate amount */ $ftime = mktime(0, 0, 0, $nhifrm_month, $nhifrm_day, $nhifrm_year); $ttime = mktime(0, 0, 0, $nhito_month, $nhito_day, $nhito_year); $days = 0; $weeks = 0; while ($ftime <= $ttime) { if (date("w", $ftime) == 0 && isset($nweekends) && $nweekends) { $days += 0.6; } else { ++$days; } $ftime += 24 * 60 * 60; } $timeunits = $days; } else { if ($nbasis == "per_hour") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $hours; $weeks = 0; if (empty($hours) || !is_numeric($hours)) { return "<li class='err'><b>ERROR</b>: Invalid amount of hours.</li>"; } } else { if ($nbasis == "per_week") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $weeks; $hours = 0; if (empty($weeks) || !is_numeric($weeks)) { return "<li class='err'><b>ERROR</b>: Invalid amount of weeks.</li>"; } } else { return "<li class='err'><b>ERROR</b>: No basis selected.</li>"; } } } /* calculate amount according to hire settings, quantity and time units */ if ($nhalf_day) { $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis) - basisPrice($cusnum, $nasset_id, $nbasis) + basisPrice($cusnum, $nasset_id, $nbasis) / 2; } else { $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis); } /* insert item */ $sql = "SELECT asset_id FROM hire.hire_invitems WHERE invid='{$invid}' AND asset_id='{$nasset_id}'"; $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets."); // No duplicate assets if (!pg_num_rows($asset_rslt)) { if (isHired($nasset_id)) { return "\r\n\t\t\t\t<li class='err'>\r\n\t\t\t\t\t<b>ERROR</b>: Asset has already hired out.\r\n\t\t\t\t</li>"; } $sql = "\r\n\t\t\t\tINSERT INTO hire.hire_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tcollection, half_day, weekends\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$collection}', '{$nhalf_day}', '{$nweekends}'\r\n\t\t\t\t)"; db_exec($sql) or errDie("Unable to create new invoice item."); $item_id = pglib_lastid("hire.hire_invitems", "id"); $sql = "\r\n\t\t\t\tINSERT INTO hire.reprint_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tcollection, half_day, weekends, item_id\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$collection}', '{$nhalf_day}', '{$nweekends}', '{$item_id}'\r\n\t\t\t\t)"; db_exec($sql) or errDie("Unable to create reprint invoice item."); } } if ($monthly == "true") { $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'"; db_exec($sql) or errDie("Unable to remove monthly items."); } else { $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}'"; $mii_rslt = db_exec($sql) or errDie("Unable to retrieve inv items."); $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'"; db_exec($sql) or errDie("Unable to remove monthly items."); while ($item = pg_fetch_array($mii_rslt)) { $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, \r\n\t\t\t\t\tbasis, hours, weeks, collection, half_day, \r\n\t\t\t\t\tweekends, item_id\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$item['invid']}', '{$item['asset_id']}', '{$item['qty']}', '{$item['amt']}', '{$item['from_date']}', '{$item['to_date']}', \r\n\t\t\t\t\t'{$item['basis']}', '{$item['hours']}', '{$item['weeks']}', '{$item['collection']}', '{$item['half_day']}', \r\n\t\t\t\t\t'{$item['weekends']}', '{$item['id']}'\r\n\t\t\t\t)"; db_exec($sql) or errDie("Unable to create monthly items."); } } $sql = "SELECT * FROM hire.reprint_invoices WHERE invid='{$invid}'"; $ri_rslt = db_exec($sql) or errDie("Unable to retrieve reprints."); // Create a new entry, or update if (pg_num_rows($ri_rslt)) { $sql = "\r\n\t\t\tUPDATE hire.reprint_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}', \r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$invb['delchrg']}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}' \r\n\t\t\tWHERE invid='{$invid}'"; db_exec($sql) or errDie("Unable to update reprint."); } else { $sql = "\r\n\t\t\tINSERT INTO hire.reprint_invoices(\r\n\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, ordno, \r\n\t\t\t\tchrgvat, terms, traddisc, salespn, odate, delchrg, \r\n\t\t\t\tsubtot, vat, total, balance, comm, printed, done, div, \r\n\t\t\t\tusername, rounding, delvat, vatnum, pcash, pcheque, \r\n\t\t\t\tpcc, pcredit\r\n\t\t\t) VALUES (\r\n\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', '{$invb['ordno']}', \r\n\t\t\t\t'{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', '{$invb['odate']}', '{$invb['delchrg']}', \r\n\t\t\t\t'{$invb['subtot']}', '{$invb['vat']}' , '{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', 'y', 'y', '" . USER_DIV . "', \r\n\t\t\t\t'" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', '{$invb['vatnum']}', '{$invb['pcash']}', '{$invb['pcheque']}', \r\n\t\t\t\t'{$invb['pcc']}', '{$invb['pcredit']}'\r\n\t\t\t)"; db_exec($sql) or errDie("Unable to add reprint."); } $sql = "SELECT * FROM hire.monthly_invoices\r\n\t\t\tWHERE invid='{$invid}' OR invnum='{$invb['invnum']}'"; $mi_rslt = db_exec($sql) or errDie("Unable to retrieve monthly."); // Should we create a new entry if (pg_num_rows($mi_rslt)) { $sql = "\r\n\t\t\tUPDATE hire.monthly_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}', \r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$invb['delchrg']}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}', \r\n\t\t\t\thire_invid='{$invid}' \r\n\t\t\tWHERE invid='{$invb['invid']}'"; } elseif (empty($monthly)) { $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invoices (\r\n\t\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, \r\n\t\t\t\t\tordno, chrgvat, terms, traddisc, salespn, odate, \r\n\t\t\t\t\tdelchrg, subtot, vat, total, balance, comm, \r\n\t\t\t\t\tprinted, done, div, username, rounding, delvat, vatnum, \r\n\t\t\t\t\tpcash, pcheque, pcc, pcredit, invoiced_month, hire_invid\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', \r\n\t\t\t\t\t'{$invb['ordno']}', '{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', '{$invb['odate']}', \r\n\t\t\t\t\t'{$invb['delchrg']}', '{$invb['subtot']}', '{$invb['vat']}' , '{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', \r\n\t\t\t\t\t'y', 'y', '" . USER_DIV . "', '" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', '{$invb['vatnum']}', \r\n\t\t\t\t\t'{$invb['pcash']}', '{$invb['pcheque']}', '{$invb['pcc']}', '{$invb['pcredit']}', '" . date("m") . "', '{$invid}'\r\n\t\t\t\t)"; } db_exec($sql) or errDie("Unable to store monthly invoice."); pglib_transaction("COMMIT"); if (isset($upBtn)) { if ($upBtn == "Return") { return returnHire(); } elseif ($upBtn == "Invoice") { return invoiceHire(); } } return false; }
function update($_POST) { extract($_POST); $collect_ar = array(); if (!empty($client_collect)) { $collect_ar[] = "Client Collect"; } if (!empty($collect)) { $collect_ar[] = "Collect"; } if (!empty($deliver)) { $collect_ar[] = "Deliver"; } $collection = implode(", ", $collect_ar); // if ((in_array("Collect", $collect_ar) && in_array("Client Collect", $collect_ar)) // || (count($collect_ar) == 3)) { // return "<li class='err'>Invalid collection options selected.</li>"; // } if (count($collect_ar) > 1 && in_array("Client Collect", $collect_ar)) { return "<li class='err'>Invalid collection options selected.</li>"; } $temp_assets = explode(",", $temp_assets); pglib_transaction("BEGIN"); // Stock Sales if (isset($snremove) && is_array($snremove)) { foreach ($snremove as $id => $value) { $sql = "DELETE FROM hire.hire_stock_items WHERE id='{$id}'"; db_exec($sql) or errDie("Unable to remove stock item."); } } if (isset($snstock) && $snstock > 0) { if (!is_numeric($snqty)) { $snqty = 1; } #get vatperc for this item $get_vatp = "SELECT vat_amount FROM vatcodes WHERE id = '{$snvatcode}' LIMIT 1"; $run_vatp = db_exec($get_vatp) or errDie("Unable to get vat code information."); if (pg_numrows($run_vatp) < 1) { $vatperc = 0; } else { $vatperc = pg_fetch_result($run_vatp, 0, 0); } // $sql = "SELECT selamt FROM cubit.stock WHERE stkid='$snstock'"; // $price_rslt = db_exec($sql) or errDie("Unable to retrieve price."); // $price = pg_fetch_result($price_rslt, 0); $price = get_excl_stock($snstock) * $snqty; $excl_price = get_excl_stock($snstock) * $snqty; $vatamount = sprint($excl_price / 100 * $vatperc); $price += 0; $price += sprint($excl_price / 100 * $vatperc); $snamt = $price * $snqty; #hack, because stores functionality isnt working ... $get_store = "SELECT whid FROM stock WHERE stkid = '{$snstock}' LIMIT 1"; $run_store = db_exec($get_store) or errDie("Unable to get stock store information."); $snstore = pg_fetch_result($run_store, 0, 0); $sql = "\r\n\t\t\tINSERT INTO hire.hire_stock_items (\r\n\t\t\t\twhid, invid, stkid, qty, vatcode, unitcost, \r\n\t\t\t\tamount, excl_amount, vatamount\r\n\t\t\t) VALUES (\r\n\t\t\t\t'{$snstore}', '{$invid}', '{$snstock}', '{$snqty}', '{$snvatcode}', '" . sprint($price / $snqty) . "', \r\n\t\t\t\t'" . sprint($snamt / $snqty) . "', '{$excl_price}', '{$vatamount}'\r\n\t\t\t)"; db_exec($sql) or errDie("Unable to add stock."); $sql = "UPDATE cubit.stock SET alloc=(alloc+'{$snqty}') WHERE stkid='{$snstock}'"; db_exec($sql) or errDie("Unable to update stock allocation."); } if (isset($nhalf_day) && $nhalf_day == "checked") { $nhalf_day = 1; } else { $nhalf_day = 0; } if (isset($nweekends) && $nweekends == "checked") { $nweekends = 1; } else { $nweekends = 0; } $sql = "UPDATE hire.hire_invoices SET comm='{$comm}' WHERE invid='{$invid}'"; $comm_rslt = db_exec($sql) or errDie("Unable to retrieve invoice."); foreach ($temp_assets as $key => $value) { $sql = "SELECT * FROM cubit.assets WHERE id='{$key}'"; $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset."); $asset_data = pg_fetch_array($asset_rslt); } if (isset($amount)) { foreach ($amount as $key => $value) { // if (empty($monthly)) { // $amount[$key] = ""; // } #redundant code ... breaks new hire if you click update multiple times (sets item amount = 0 after second update) // if (!user_is_admin(USER_ID)) { // $amount[$key] = ""; // } if (!isset($half_day[$key]) || empty($half_day[$key])) { $half_day[$key] = 0; } if (!isset($weekends[$key]) || empty($weekends[$key])) { $weekends[$key] = 0; } else { $weekends[$key] = 1; } if (!isset($total_days[$key]) || empty($total_days[$key])) { $epoch_from = strtotime($from_date[$key]); $epoch_to = strtotime($to_date[$key]); $total_days[$key] = ($epoch_to - $epoch_from) / (60 * 60 * 24) + 1; $total_days[$key] = ceil($total_days[$key]); } $sql = "SELECT qty, total_days, half_day FROM hire.hire_invitems WHERE id='{$key}'"; $old_rslt = db_exec($sql) or errDie("Unable to retrieve old qty."); list($oldqty, $olddays, $oldhalf_day) = pg_fetch_array($old_rslt); if (!isset($oldqty)) { $oldqty = 1; } if (!isset($qty[$key])) { $oldqty = 1; $qty[$key] = 1; } if ($half_day[$key]) { $half_day[$key] = 1; $recalc = FALSE; } else { $half_day[$key] = 0; } if (empty($amount) && $amount != 0 || $oldqty != $qty[$key] || $olddays != $total_days[$key] || (!isset($amount[$key]) or empty($amount[$key])) || $half_day == 1) { if ($basis[$key] == "per_day") { $to_time = strtotime($from_date[$key]) + $total_days[$key] * (60 * 60 * 24); $hifrm = "{$mfrm_year[$key]}-{$mfrm_month[$key]}-{$mfrm_day[$key]}"; $hito = date("Y-m-d", $to_time); $hours = "0"; /* calculate amount */ $ftime = getDTEpoch("{$hifrm} 0:00:00"); $ttime = getDTEpoch("{$hito} 0:00:00"); $days = 0; $weeks = 0; $months = 0; while ($ftime <= $ttime) { if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) { $days += 0.6; } else { ++$days; } $ftime += 24 * 60 * 60; } $timeunits = $total_days[$key]; } else { if ($basis[$key] == "per_hour") { $hifrm = $hito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $hours; $weeks = 0; $months = 0; if (empty($hours) || !is_numeric($hours)) { return "<li class='err'><b>ERROR</b>: Invalid amount of hours.</li>"; } } else { if ($nbasis == "per_week") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $weeks; $hours = 0; $months = 0; if (empty($weeks) || !is_numeric($weeks)) { return "<li class='err'><b>ERROR</b>: Invalid amount of weeks.</li>"; } } else { if ($nbasis == "per_month") { $nhifrm = $nhito = mkDate($pinv_year, $pinv_month, $pinv_day); $timeunits = $months; $weeks = 0; $hours = 0; if (empty($months) || !is_numeric($months)) { return "<li class='err'><b>ERROR</b>: Invalid amount of months.</li>"; } } } } } if ($half_day[$key]) { // $amount[$key] = ($qty[$key] * $timeunits * (basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key]) - (basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key]) + ((basisPrice($cusnum, $asset_id[$key], $basis[$key]) * $qty[$key])) / halfday_rate()); $amount[$key] = sprint($qty[$key] * ($timeunits * basisPrice($cusnum, $asset_id[$key], $basis[$key]) - basisPrice($cusnum, $asset_id[$key], $basis[$key]) + basisPrice($cusnum, $asset_id[$key], $basis[$key]) / halfday_rate())); } else { $amount[$key] = $qty[$key] * $timeunits * basisPrice($cusnum, $asset_id[$key], $basis[$key]); } } if ($amount[$key] == 0) { $amount[$key] = 0; $blank_amount = 1; } else { $blank_amount = 0; } $sql = "\r\n\t\t\t\tUPDATE hire.hire_invitems \r\n\t\t\t\tSET amt='{$amount[$key]}', half_day='{$half_day[$key]}', weekends='{$weekends[$key]}', \r\n\t\t\t\t\ttotal_days='{$total_days[$key]}', qty='{$qty[$key]}' \r\n\t\t\t\tWHERE id='{$key}'"; db_exec($sql) or errDie("Unable to update item amount."); $sql = "\r\n\t\t\t\tUPDATE hire.reprint_invitems \r\n\t\t\t\tSET amt='{$amount[$key]}', half_day='{$half_day[$key]}', weekends='{$weekends[$key]}' \r\n\t\t\t\tWHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to update return item amount."); if ($blank_amount) { $amount[$key] = ""; } //$hifrm = "$hifrm_year[$key]-$hifrm_month[$key]-$hifrm_day[$key]"; //$hito = "$hito_year[$key]-$hito_month[$key]-$hito_day[$key]"; if (!isset($remove[$key])) { $sql = "SELECT basis FROM hire.hire_invitems WHERE id='{$key}'"; $item_rslt = db_exec($sql) or errDie("Unable to retrieve basis."); $mbasis = pg_fetch_result($item_rslt, 0); /* determine time units */ if ($mbasis == "per_day") { $mfrm = mkdate($mfrm_year[$key], $mfrm_month[$key], $mfrm_day[$key]); $mto = mkdate($mto_year[$key], $mto_month[$key], $mto_day[$key]); /* calculate amount */ $ftime = mktime(0, 0, 0, $mfrm_month[$key], $mfrm_day[$key], $mfrm_year[$key]); $ttime = mktime(0, 0, 0, $mto_month[$key], $mto_day[$key], $mto_year[$key]); $days = 0; if (empty($weeks)) { $weeks = 0; } if (empty($hours)) { $hours = 0; } if (empty($months)) { $months = 0; } while ($ftime <= $ttime) { if (date("w", $ftime) == 0 && isset($weekends[$key]) && $weekends[$key]) { $days += 0.6; } else { ++$days; } $ftime += 24 * 60 * 60; } $timeunits = $days; $sql = "\r\n\t\t\t\t\t\tUPDATE hire.hire_invitems \r\n\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}' \r\n\t\t\t\t\t\tWHERE id='{$key}'"; db_exec($sql) or errDie("Unable to update items."); $sql = "\r\n\t\t\t\t\t\tUPDATE hire.reprint_invitems \r\n\t\t\t\t\t\tSET from_date='{$mfrm}', to_date='{$mto}' \r\n\t\t\t\t\t\tWHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to update reprint items."); } } else { // Delete the old items $sql = "DELETE FROM hire.hire_invitems WHERE id='{$key}'"; db_exec($sql) or errDie("Unable to remove old items."); $sql = "DELETE FROM hire.reprint_invitems WHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to remove old reprint items."); //.Remove if the item has been hired as well $sql = "DELETE FROM hire.assets_hired WHERE item_id='{$key}'"; db_exec($sql) or errDie("Unable to remove items from hired log."); } } } $sql = "SELECT * FROM hire.hire_invoices WHERE invid='{$invid}'"; $hi_rslt = db_exec($sql) or errDie("Unable to retrieve invoice."); $invb = pg_fetch_array($hi_rslt); // Default basis if (!empty($nasset_id) && $nbasis == "0") { list($serialqty, $tasset_id) = explode(":", $nasset_id); $nbasis = default_basis($tasset_id); } // Insert new items if ($nasset_id != "0") { if ($nasset_id == "0") { return "<li class='err'><b>ERROR</b>: No asset selected.</li>"; } /* get asset id */ list($serialqty, $nasset_id) = explode(":", $nasset_id); /* disabled items don't get passed through */ if ($serialqty == "s" || !isset($nqty)) { $nqty = "1"; } else { $sql = "SELECT serial2 FROM cubit.assets WHERE id='{$nasset_id}'"; $dqty_rslt = db_exec($sql) or errDie("Unable to retrieve assets."); $dqty = pg_fetch_result($dqty_rslt, 0); if ($dqty - $nqty < 0) { return "<li class='err'><b>ERROR</b>: Invalid quantity. Only <b>{$dqty}</b> available.</li>"; } } if (empty($nqty) || !is_numeric($nqty)) { return "<li class='err'><b>ERROR</b>: Invalid quantity</li>"; } // Default basis if ($nbasis == "0") { $nbasis = default_basis($nasset_id); } /* determine time units */ if ($nbasis == "per_day") { $nhifrm = mkdate($nhifrm_year, $nhifrm_month, $nhifrm_day); $nhito = mkdate($nhito_year, $nhito_month, $nhito_day); $hours = "0"; /* calculate amount */ $ftime = mktime(0, 0, 0, $nhifrm_month, $nhifrm_day, $nhifrm_year); $ttime = mktime(0, 0, 0, $nhito_month, $nhito_day, $nhito_year); $days = 0; $weeks = 0; $months = 0; while ($ftime <= $ttime) { if (date("w", $ftime) == 0 && isset($nweekends) && $nweekends) { $days += 0.6; } else { ++$days; } $ftime += 24 * 60 * 60; } $timeunits = $days; } else { if ($nbasis == "per_hour") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $hours; $weeks = 0; $months = 0; if (empty($hours) || !is_numeric($hours)) { return "<li class='err'><b>ERROR</b>: Invalid amount of hours.</li>"; } } else { if ($nbasis == "per_week") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $weeks; $hours = 0; $months = 0; if (empty($weeks) || !is_numeric($weeks)) { return "<li class='err'><b>ERROR</b>: Invalid amount of weeks.</li>"; } } else { if ($nbasis == "per_month") { $nhifrm = $nhito = mkdate($pinv_year, $pinv_month, $pinv_day); $timeunits = $months; $hours = 0; $weeks = 0; if (empty($months) || !is_numeric($months)) { return "<li class='err'><b>ERROR</b>: Invalid amount of months.</li>"; } } else { return "<li class='err'><b>ERROR</b>: No basis selected.</li>"; } } } } /* calculate amount according to hire settings, quantity and time units */ if ($nhalf_day) { $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis) - basisPrice($cusnum, $nasset_id, $nbasis) + basisPrice($cusnum, $nasset_id, $nbasis) / halfday_rate(); } else { $camt = $nqty * $timeunits * basisPrice($cusnum, $nasset_id, $nbasis); } /* insert item */ $sql = "SELECT asset_id FROM hire.hire_invitems\r\n\t\t\t\tWHERE invid='{$invid}' AND asset_id='{$nasset_id}'"; $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets."); // No duplicate assets if (!pg_num_rows($asset_rslt) || !isSerialized($nasset_id)) { if (isHired($nasset_id)) { return "\r\n\t\t\t\t<li class='err'>\r\n\t\t\t\t\t<b>ERROR</b>: Asset has already hired out.\r\n\t\t\t\t</li>"; } $sql = "\r\n\t\t\t\tINSERT INTO hire.hire_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tmonths, collection, half_day, weekends\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$months}', '{$collection}', '{$nhalf_day}', '{$nweekends}'\r\n\t\t\t\t)"; db_exec($sql) or errDie("Unable to create new invoice item."); $item_id = pglib_lastid("hire.hire_invitems", "id"); $sql = "\r\n\t\t\t\tINSERT INTO hire.reprint_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, basis, hours, weeks, \r\n\t\t\t\t\tmonths, collection, half_day, weekends, item_id\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$nasset_id}', '{$nqty}', '{$camt}', '{$nhifrm}', '{$nhito}', '{$nbasis}', '{$hours}', '{$weeks}', \r\n\t\t\t\t\t'{$months}', '{$collection}', '{$nhalf_day}', '{$nweekends}', '{$item_id}'\r\n\t\t\t\t)"; db_exec($sql) or errDie("Unable to create reprint invoice item."); } } if ($monthly == "true") { $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'"; db_exec($sql) or errDie("Unable to remove monthly items."); } else { $sql = "SELECT * FROM hire.hire_invitems WHERE invid='{$invid}'"; $mii_rslt = db_exec($sql) or errDie("Unable to retrieve inv items."); $sql = "DELETE FROM hire.monthly_invitems WHERE invid='{$invid}'"; db_exec($sql) or errDie("Unable to remove monthly items."); while ($item = pg_fetch_array($mii_rslt)) { $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invitems (\r\n\t\t\t\t\tinvid, asset_id, qty, amt, from_date, to_date, \r\n\t\t\t\t\tbasis, hours, weeks, months, collection, half_day, \r\n\t\t\t\t\tweekends, item_id, invnum\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$item['invid']}', '{$item['asset_id']}', '{$item['qty']}', '{$item['amt']}', '{$item['from_date']}', '{$item['to_date']}', \r\n\t\t\t\t\t'{$item['basis']}', '{$item['hours']}', '{$item['weeks']}', '{$item['months']}', '{$item['collection']}', '{$item['half_day']}',\r\n\t\t\t\t\t'{$item['weekends']}', '{$item['id']}', '{$invb['invnum']}'\r\n\t\t\t\t)"; db_exec($sql) or errDie("Unable to create monthly items."); } } $sql = "SELECT * FROM hire.reprint_invoices WHERE invid='{$invid}'"; $ri_rslt = db_exec($sql) or errDie("Unable to retrieve reprints."); // Create a new entry, or update if (pg_num_rows($ri_rslt)) { $sql = "\r\n\t\t\tUPDATE hire.reprint_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}',\r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$delchrg}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}' \r\n\t\t\tWHERE invid='{$invid}'"; db_exec($sql) or errDie("Unable to update reprint."); } else { $sql = "\r\n\t\t\tINSERT INTO hire.reprint_invoices (\r\n\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, ordno, \r\n\t\t\t\tchrgvat, terms, traddisc, salespn, odate, delchrg, \r\n\t\t\t\tsubtot, vat, total, balance, comm, printed, done, div, \r\n\t\t\t\tusername, rounding, delvat, vatnum, pcash, pcheque, \r\n\t\t\t\tpcc, pcredit\r\n\t\t\t) VALUES (\r\n\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', '{$invb['ordno']}',\r\n\t\t\t\t'{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', '{$invb['odate']}', '{$invb['delchrg']}', \r\n\t\t\t\t'{$invb['subtot']}', '{$invb['vat']}' , '{$invb['total']}', '{$invb['balance']}', '{$invb['comm']}', 'y', 'y', '" . USER_DIV . "', \r\n\t\t\t\t'" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', '{$invb['vatnum']}', '{$invb['pcash']}', '{$invb['pcheque']}', \r\n\t\t\t\t'{$invb['pcc']}', '{$invb['pcredit']}'\r\n\t\t\t)"; db_exec($sql) or errDie("Unable to add reprint."); } $sql = "SELECT * FROM hire.monthly_invoices WHERE invid='{$invid}' OR invnum='{$invb['invnum']}' AND invnum!=0"; $mi_rslt = db_exec($sql) or errDie("Unable to retrieve monthly."); // Should we create a new entry if (pg_num_rows($mi_rslt)) { $sql = "\r\n\t\t\tUPDATE hire.monthly_invoices \r\n\t\t\tSET deptid='{$invb['deptid']}', cusnum='{$invb['cusnum']}', deptname='{$invb['deptname']}', cusacc='{$invb['cusacc']}', \r\n\t\t\t\tcusname='{$invb['cusname']}', surname='{$invb['surname']}', cusaddr='{$invb['cusaddr']}', cusvatno='{$invb['cusvatno']}', \r\n\t\t\t\tcordno='{$invb['cordno']}', ordno='{$invb['ordno']}', chrgvat='{$invb['chrgvat']}', terms='{$invb['terms']}', \r\n\t\t\t\ttraddisc='{$invb['traddisc']}', salespn='{$invb['salespn']}', odate='{$invb['odate']}', delchrg='{$invb['delchrg']}', \r\n\t\t\t\tsubtot='{$invb['subtot']}', vat='{$invb['vat']}', total='{$invb['total']}', balance='{$invb['balance']}', \r\n\t\t\t\tcomm='{$invb['comm']}', printed='{$invb['printed']}', done='{$invb['done']}', div='{$invb['div']}', \r\n\t\t\t\tusername='******'username']}', rounding='{$invb['rounding']}', delvat='{$invb['delvat']}', vatnum='{$invb['vatnum']}', \r\n\t\t\t\tpcash='{$invb['pcash']}', pcheque='{$invb['pcheque']}', pcc='{$invb['pcc']}', pcredit='{$invb['pcredit']}', \r\n\t\t\t\thire_invid='{$invid}' \r\n\t\t\tWHERE invid='{$invb['invid']}'"; } elseif (empty($monthly)) { $sql = "\r\n\t\t\t\tINSERT INTO hire.monthly_invoices (\r\n\t\t\t\t\tinvid, invnum, deptid, cusnum, deptname, cusacc, \r\n\t\t\t\t\tcusname, surname, cusaddr, cusvatno, cordno, \r\n\t\t\t\t\tordno, chrgvat, terms, traddisc, salespn, \r\n\t\t\t\t\todate, delchrg, subtot, vat, total, balance, \r\n\t\t\t\t\tcomm, printed, done, div, username, rounding, delvat, \r\n\t\t\t\t\tvatnum, pcash, pcheque, pcc, pcredit, invoiced_month, \r\n\t\t\t\t\thire_invid\r\n\t\t\t\t) VALUES (\r\n\t\t\t\t\t'{$invid}', '{$invb['invnum']}', '{$invb['deptid']}', '{$invb['cusnum']}', '{$invb['deptname']}', '{$invb['cusacc']}', \r\n\t\t\t\t\t'{$invb['cusname']}', '{$invb['surname']}', '{$invb['cusaddr']}', '{$invb['cusvatno']}', '{$invb['cordno']}', \r\n\t\t\t\t\t'{$invb['ordno']}', '{$invb['chrgvat']}', '{$invb['terms']}', '{$invb['traddisc']}', '{$invb['salespn']}', \r\n\t\t\t\t\t'{$invb['odate']}', '{$invb['delchrg']}', '{$invb['subtot']}', '{$invb['vat']}', '{$invb['total']}', '{$invb['balance']}', \r\n\t\t\t\t\t'{$invb['comm']}', 'y', 'y', '" . USER_DIV . "', '" . USER_NAME . "', '{$invb['rounding']}', '{$invb['delvat']}', \r\n\t\t\t\t\t'{$invb['vatnum']}', '{$invb['pcash']}', '{$invb['pcheque']}', '{$invb['pcc']}', '{$invb['pcredit']}', '" . date("m") . "', \r\n\t\t\t\t\t'{$invb['invid']}' \r\n\t\t\t\t)"; } db_exec($sql) or errDie("Unable to store monthly invoice."); pglib_transaction("COMMIT"); if (!isSerialized($nasset_id) && (!isset($_SESSION["ns"]) || !$_SESSION["ns"])) { $_SESSION["ns"] = 1; } else { $_SESSION["ns"] = 0; } if (isset($upBtn)) { if ($upBtn == "Return") { return returnHire(); } elseif ($upBtn == "Invoice") { return invoiceHire(); } } return false; }