function mkTTorder($id, $evids, $trans_id) { $tt = getOneTT($id, $false); $vendcust = $tt['tab'] == 'C' ? 'customer' : 'vendor'; //Steuerzone ermitteln (0-3) $sql = "SELECT taxzone_id FROM " . $vendcust . " WHERE id = " . $tt["fid"]; $rs = $_SESSION['db']->getOne($sql); $tzid = $rs["taxzone_id"]; $TAX = getTax($tzid); //Artikeldaten holen $sql = "SELECT * FROM parts WHERE partnumber = '" . $_SESSION['ttpart'] . "'"; $part = $_SESSION['db']->getOne($sql); $partid = $part["id"]; $sellprice = $part["sellprice"]; $unit = $part["unit"]; //Steuersatz ermitteln $tax = $TAX[$part["buchungsgruppen_id"]]; $curr = getCurr(True); //Events holen $events = getTTEvents($id, false, $evids, True); if (!$events) { return ".:nothing to do:."; } if (!$evids) { $evids = 'and t.id in ('; foreach ($events as $row) { $tmp[] = $row['id']; } $evids .= implode(',', $tmp) . ') '; } $_SESSION['db']->begin(); if ($trans_id < 1) { //Auftrag erzeugen $sonumber = $tt['tab'] == 'C' ? nextNumber("sonumber") : nextNumber("ponumber"); if (!$sonumber) { return ".:error:."; } $sql = "INSERT INTO oe (notes,transaction_description,ordnumber," . $vendcust . "_id,taxincluded,currency_id) "; $sql .= "VALUES ('" . $tt["ttdescription"] . "','" . $tt["ttname"] . "',{$sonumber},'" . $tt["fid"] . "','f',"; $sql .= "coalesce((SELECT currency_id FROM " . $vendcust . " WHERE id = " . $tt['fid'] . "),{$curr}))"; $rc = $_SESSION['db']->query($sql, "newOE"); if (!$rc) { $sql = "DELETE FROM oe WHERE ordnumber = '{$sonumber}'"; $rc = $_SESSION['db']->query($sql, "delOE"); return ".:error:. 0"; } $sql = "SELECT id FROM oe WHERE ordnumber = '{$sonumber}'"; $rs = $_SESSION['db']->getOne($sql); $trans_id = $rs["id"]; if ($trans_id <= 0) { $sql = "DELETE FROM oe WHERE ordnumber = '{$sonumber}'"; $rc = $_SESSION['db']->query($sql, "delOE"); return ".:error:. 0"; } $netamount = 0; } else { $sql = "SELECT * from oe WHERE id = " . $trans_id; $rc = $_SESSION['db']->getOne($sql, ''); if (!$rc) { return ".:error:. 00"; } $netamount = $rc['netamount']; } //$sql_i = 'INSERT INTO orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount,serialnumber,reqdate) values ('; $fields = array('trans_id', 'parts_id', 'description', 'qty', 'sellprice', 'unit', 'ship', 'discount', 'serialnumber', 'reqdate'); foreach ($events as $row) { if ($row["ttstop"] == "") { $_SESSION['db']->rollback(); return ".:close event:."; } $t1 = strtotime($row["ttstart"]); $t2 = strtotime($row["ttstop"]); //Minuten $diff = floor(($t2 - $t1) / 60); //Abrechnungseinheiten $time = floor($diff / $_SESSION['tttime']); //Ist der Rest über der Tolleranz if ($diff - $_SESSION['tttime'] * $time > $_SESSION['ttround']) { $time++; } $price = $time * $sellprice; //Orderitemseintrag $rqdate = substr($row['ttstop'], 0, 10); $values = array($trans_id, $partid, $row["ttevent"], $time, $sellprice, $unit, 0, 0, $diff, $rqdate); //$sql = $sql_i."$trans_id,$partid,'".$row["ttevent"]."',$time,$sellprice,'$unit',0,0,'$diff','".substr($row['ttstop'],0,10)."')"; //$rc = $_SESSION['db']->query($sql); $rc = $_SESSION['db']->insert('orderitems', $fields, $values); if (!$rc) { $_SESSION['db']->rollback(); return ".:error:. 1"; } $netamount += $price; $amount += $price * $tax; $parts = getTTparts($row["id"]); if ($parts) { foreach ($parts as $part) { $values = array($trans_id, $part['parts_id'], $part['parts_txt'], $part['qty'], $part['sellprice'], $part['unit'], 0, 0, Null, $rqdate); $rc = $_SESSION['db']->insert('orderitems', $fields, $values); if (!$rc) { $_SESSION['db']->rollback(); return ".:error:. 2"; } $netamount += $part['qty'] * $part['sellprice']; $amount += $part['qty'] * $part['sellprice'] * $TAX[$part['buchungsgruppen_id']]; } } } //OE-Eintrag updaten $nun = date('Y-m-d'); //$amount = $netamount + $mwst; $fields = array('transdate', 'amount', 'netamount', 'reqdate', 'notes', 'employee_id'); $values = array($nun, $amount, $netamount, $nun, $tt["ttdescription"], $_SESSION["loginCRM"]); $rc = $_SESSION['db']->update('oe', $fields, $values, 'id = ' . $trans_id); if (!$rc) { $_SESSION['db']->rollback(); return ".:error:. 2"; } else { //Events als Abgerechnet markieren. $sql = "UPDATE tt_event t set cleared = {$trans_id} where t.ttid = {$id} {$evids}"; $rc = $_SESSION['db']->query($sql); $_SESSION['db']->commit(); return ".:ok:."; } }
function listTevents($id, $fid = 0, $tab) { $events = getTTEvents($id, "a", false); if ($tab == 'C') { $link = "<a href='../oe.pl?action=edit&type=sales_order&vc=customer&id=%d&callback=crm/timetrack.php%%3ffid={$fid}'>"; } else { $link = "<a href='../oe.pl?action=edit&type=purchase_order&vc=vendor&id=%d&callback=crm/timetrack.php%%3ffid={$fid}'>"; } if (!$events) { echo json_encode(array('ok' => 0)); } $tt = getOneTT($events[0]["ttid"]); $liste = "<form name='cleared' method='post' action='timetrack.php'>"; $liste .= "<input type='hidden' name='tid' value='" . $events[0]["ttid"] . "'>"; $liste .= "<table>"; $i = 0; $now = time(); $diff = 0; $lastcleared = 0; if ($events) { foreach ($events as $row) { $a = explode(" ", $row["ttstart"]); $t1 = strtotime($row["ttstart"]); if ($row["ttstop"]) { $b = explode(" ", $row["ttstop"]); $stop = db2date($b[0]) . " " . substr($b[1], 0, 5); $t2 = strtotime($row["ttstop"]); } else { if ($t1 <= $now) { $t2 = $now; } else { $t2 = $t1; } $stop = "<a href='timetrack.php?tid=" . $row["ttid"] . "&eventid=" . $row["id"] . "&stop=now'><b>" . translate('.:stop now:.', 'work') . "</b></a>"; } $min = $t2 - $t1; $diff += $min; $i++; if ($row["cleared"] > 0) { $clear = "<td class='klein'>-</td>"; if ($row['cleared'] > $lastcleared) { $lastcleared = $row['cleared']; } } else { if ($row["uid"] == $_SESSION["loginCRM"]) { $clear = "<td><input type='checkbox' name='clear[]' value='" . $row["id"] . "'></td>"; } else { $clear = "<td>+</td>"; } } $liste .= "<tr class='calls" . $i % 2 . "' onClick='editrow(" . $row["id"] . ");'>{$clear}<td>" . db2date($a[0]) . " " . substr($a[1], 0, 5) . "</td>"; $liste .= "<td>" . $stop . "</td><td align='right'>" . floor($min / 60) . "</td><td>" . $row["user"] . "</td><td>"; if (strlen($row["ttevent"]) > 40) { $liste .= substr($row["ttevent"], 0, 40) . "...</td><td>"; } else { $liste .= $row["ttevent"] . "</td><td>"; } $liste .= sprintf($link, $row['cleared']) . $row["ordnumber"] . "</a>"; if ($row['closed'] == 't') { $liste .= "*"; } $liste .= "</td></tr>"; } } $diff = floor($diff / 60); if ($tt["aim"] > 0) { $rest = $tt["aim"] * 60 - $diff; $rstd = floor($rest / 60); $rmin = $rest % 60; $rest = translate('.:remain:.', 'work') . " {$rstd}:{$rmin} " . translate('.:hours:.', 'work'); } else { $rest = ""; } if ($diff > 60) { $min = sprintf("%02d", $diff % 60); $std = floor($diff / 60); $use = "{$std}:{$min} " . translate('.:hours:.', 'work'); } else { $use = $diff . " " . translate('.:minutes:.', 'work'); } $liste .= "</table><input type='checkbox' name='clrok' value='1'>" . translate(".:all:.", 'work') . " "; if ($lastcleared > 0) { $liste .= "<input type='radio' name='order' value='0' checked>" . translate('.:new Order:.', 'work') . ' '; $liste .= "<input type='radio' name='order' value='{$lastcleared}'>" . translate('.:add to last Order:.', 'work') . ' '; } $liste .= "<input type='submit' name='clr' value='" . translate(".:clearing:.", "work") . "'></form>"; echo json_encode(array('liste' => $liste, 'used' => $use, 'rest' => $rest, 'ok' => 1)); }