setupCustomFields('EcmStockOperations');
foreach ($this->field_defs as $field) {
$this->field_name_map[$field['name']] = $field;
}
}
function get_summary_text() {
return $this->name;
}
function create_list_query($order_by, $where, $show_deleted = 0) {
$custom_join = $this->custom_fields->getJOIN();
$query = "SELECT ";
$query.="ecmstockoperations.*,users.user_name as assigned_user_name";
if ($custom_join)
$query.=$custom_join['select'];
$query.=" FROM ecmstockoperations ";
$query.="LEFT JOIN users ON ecmstockoperations.assigned_user_id=users.id";
$query.=" ";
if ($custom_join)
$query.=$custom_join['join'];
$where_auto = '1=1';
if ($show_deleted == 0)
$where_auto = " $this->table_name.deleted=0 ";
elseif ($show_deleted == 1)
$where_auto = " $this->table_name.deleted=1 ";
if ($where != "")
$query.="where $where AND " . $where_auto;
else
$query.="where " . $where_auto;
if (substr_count($order_by, '.') > 0)
$query .= " ORDER BY $order_by";
elseif ($order_by != "")
$query .= " ORDER BY $order_by";
else
$query .= " ORDER BY ecmstockoperations.name";
return $query;
}
function create_export_query($order_by, $where) {
$custom_join = $this->custom_fields->getJOIN();
$query = "SELECT ";
$query.="ecmstockoperations.*,users.user_name as assigned_user_name";
if ($custom_join)
$query.=$custom_join['select'];
$query.=" FROM ecmstockoperations ";
$query.="LEFT JOIN users ON ecmstockoperations.assigned_user_id=users.id";
$query.=" ";
if ($custom_join)
$query.=$custom_join['join'];
$where_auto = '1=1';
if ($show_deleted == 0)
$where_auto = " $this->table_name.deleted=0 ";
elseif ($show_deleted == 1)
$where_auto = " $this->table_name.deleted=1 ";
if ($where != "")
$query.="where $where AND " . $where_auto;
else
$query.="where " . $where_auto;
if (substr_count($order_by, '.') > 0)
$query .= " ORDER BY $order_by";
elseif ($order_by != "")
$query .= " ORDER BY $order_by";
else
$query .= " ORDER BY ecmstockoperations.name";
return $query;
}
function fill_in_additional_list_fields() {
}
function fill_in_additional_detail_fields() {
parent::fill_in_additional_detail_fields();
}
function get_list_view_data() {
$doc_name=array('EcmStockDocInsideIns'=>'PW','EcmStockDocMoves'=>'MM','EcmStockDocCorrects'=>'KS','EcmStockDocIns'=>'PZ','EcmStockDocOuts'=>'WZ','EcmStockDocInsideOuts'=>'RW','EcmInvoiceOuts'=>'FK');
global $current_language;
$db=$GLOBALS['db'];
$the_array = parent::get_list_view_data();
$app_list_strings = return_app_list_strings_language($current_language);
$mod_strings = return_module_language($current_language, 'EcmStockOperations');
$this->retrieve($this->id);
$mod_strings_doc = return_module_language($current_language, $this->parent_type);
$the_array['PARENT_TYPE'] = $this->parent_type;
$the_array['PARENT_TYPE_NAME'] = $mod_strings_doc['LBL_MODULE_NAME'];
$the_array['NAME'] = (($this->name == "") ? "blank " : $this->name);
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select parent_name,parent_id,parent_type,part_no,part_no2,part_no3,part_no4,part_no5,type from ecmstockoperations where id='" . $this->id . "'"));
$mod=substr($this->parent_type, 0, -1);
$nr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select counter from ecmstockoperations where id='".$this->id."'"));
$res=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("
select
sum(CASE
WHEN (type=0)
then @t :=+quantity
else @t :=-quantity
end) as tt
from ecmstockoperations where product_id='".$this->product_id."' and stock_id='".$this->stock_id."' and counter <= '".$nr['counter']."'"));
$n= new $mod();
$n->retrieve($this->parent_id);
$the_array['DOCUMENT_NAME'] = $doc_name[$this->parent_type].' '.$n->document_no;
$the_array['PREVIEW_PDF'] = ' ';
$the_array['COUNTER']=$this->date_modified;
$the_array['STOCK_NAME'] = '' . $this->stock_name . ' ';
$the_array['ENCODED_NAME'] = $this->name;
$p = new EcmProduct ();
$p->retrieve ( $this->product_id );
$the_array ['QUANTITY'] = number_format($this->quantity,$app_list_strings['ecmproducts_unit_dom_precision'] [$p->unit_id],',','');
$the_array['STATE']= number_format($res['tt'],$app_list_strings['ecmproducts_unit_dom_precision'] [$p->unit_id],',','');
$the_array['TYPE']=$this->type==1 ? 'Wyjście ' : 'Wejście ';
return $the_array;
}
function build_generic_where_clause($the_query_string) {
$where_clauses = array();
$the_query_string = PearDatabase::quote(from_html($the_query_string));
array_push($where_clauses, "ecmstockoperations.name like '$the_query_string%'");
$the_where = "";
foreach ($where_clauses as $clause) {
if ($the_where != "")
$the_where.=" or ";
$the_where.=$clause;
}
return $the_where;
}
function set_notification_body($xtpl, $ecmstockoperation) {
global $mod_strings, $app_list_strings;
$xtpl->assign("ECMSTOCKOPERATION_SUBJECT", $ecmstockoperation->name);
return $xtpl;
}
function bean_implements($interface) {
switch ($interface) {
case 'ACL':return true;
}
return false;
}
function save($check_notify = FALSE) {
$id = parent::save($check_notify);
$class_name = substr($this->parent_type, 0, -1);
$obj = new $class_name;
$obj->retrieve($this->parent_id);
$queryupdate = 'UPDATE ecmstockoperations SET date_entered = "' . date("Y-m-d", strtotime($obj->register_date)) .' 00:00:03" WHERE id = "' . $this->id . '"';
$this->db->query($queryupdate);
return $id;
}
function deleteProducts($p, $id, $itemid, $stock_id, $date) {
$arr = array();
$w = $GLOBALS['db']->query("select quantity as qty,id,price from ecmstockoperations where deleted='0' and type='0' and used=0 and in_id IS NULL and stock_id='" . $stock_id . "' and product_id='" . $p['id'] . "'");
while ($rrr = $GLOBALS['db']->fetchByAssoc($w)) {
$ww = $GLOBALS['db']->query("select quantity from ecmstockoperations where in_id='" . $rrr['id'] . "' and type='1' and in_id IS NOT NULL and stock_id='" . $stock_id . "' and product_id='" . $p['id'] . "' and deleted='0'");
$rqty = 0;
while ($rrp = $GLOBALS['db']->fetchByAssoc($ww)) {
$rqty+=$rrp['quantity'];
}
$qty = $rrr['qty'] - $rqty;
if ($qty > 0) {
$arr[] = array("qty" => $qty, "id" => $rrr['id'], "price" => $rrr['price']);
}
}
$rr = array();
$used_qty = 0;
$stop = 0;
foreach ($arr as $value) {
if ($p['quantity'] - $used_qty <= $value['qty']) {
$rr[] = array("id" => $value['id'], "qty" => $p['quantity'] - $used_qty, "price" => $value['price']);
$stop = 1;
} else {
$rr[] = array("id" => $value['id'], "qty" => $value['qty'], "price" => $value['price']);
$used_qty+=$value['qty'];
}
if ($stop) {
$stop = 0;
break;
}
}
$qq = 0;
$pp = 0;
$avg_pq = 0;
$avg_q = 0;
foreach ($rr as $r) {
$this->addDeleteOperation($r['id'], $p['id'], $r['qty'], $r['price'], $date, $id, $stock_id, $itemid);
$avg_pq+=$r['qty'] * $r['price'];
$avg_q+=$r['qty'];
}
$GLOBALS['db']->query("update ecmstockdocoutitems set price='" . ($avg_pq / $avg_q) . "' where id='" . $itemid . "'");
}
function addDeleteOperation($stock_in_id, $product_id, $quantity, $price, $date, $wz_id, $stock_id, $itemid) {
require_once("modules/EcmStockOperations/EcmStockOperation.php");
$o = new EcmStockOperation();
$o->unformat_all_fields();
require_once("modules/EcmProducts/EcmProduct.php");
$o->product_id = $product_id;
$p = new EcmProduct();
$p->retrieve($product_id);
$o->product_code = $p->code;
$o->product_name = $p->name;
require_once("modules/EcmStocks/EcmStock.php");
$o->stock_id = $stock_id;
$s = new EcmStock();
$s->retrieve($stock_id);
$o->stock_name = $s->name;
$o->parent_id = $wz_id;
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select document_no from ecmstockdocouts where id='" . $wz_id . "'"));
$o->parent_name = $r['document_no'];
$o->parent_type = "EcmStockDocOuts";
$o->quantity = $quantity;
$o->price = $price;
$o->in_id = $stock_in_id;
$o->documentitem_id = $itemid;
$o->type = 1;
$o->save();
require_once("modules/EcmStockStates/EcmStockState.php");
$ss = new EcmStockState();
$ss->UpdateStockState($o->stock_id, $o->product_id);
}
static function saveProductReservation($product_id, $stock_id, $doc_id, $doc_type, $item_id, $quantity) {
global $current_user;
$db = $GLOBALS['db'];
//check params
if (strlen($stock_id)!='36' ||
strlen($product_id)!='36' ||
strlen($doc_id)!='36' ||
strlen($item_id)!='36' ||
$doc_id == "" ||
floatval($quantity) == 0) return false;
//save reservation
$query = "
INSERT INTO ecmreservations VALUES (
'".create_guid()."',
'$product_id',
'$stock_id',
'$doc_id',
'$doc_type',
'$item_id',
'".floatval($quantity)."',
'".$current_user->id."',
'".date("Y-m-d H:i:s")."')";
return $db->query($query);
}
static function deleteDocumentReservation($doc_id, $doc_type) {
return $GLOBALS['db']->query("DELETE FROM ecmreservations WHERE document_id='$doc_id' AND document_type='$doc_type'");
}
static function getDocumentItemReservation($doc_id, $doc_type, $item_id) {
$db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT quantity FROM ecmreservations WHERE document_id='$doc_id' AND document_type='$doc_type' AND document_item_id='$item_id'"));
return floatval($res['quantity']);
}
function saveParentReservation($stock_id, $product_id, $doc_id, $item_id, $quantity, $doc_type) {
global $db, $current_user;
session_start();
$q = "select count(*) as cnt from ecmreservations where item_id='$item_id' and doc_id='$doc_id'";
$r = $db->fetchByAssoc($db->query($q));
if ($r['cnt'] > 0) {
$updateQuery =
'
UPDATE
ecmreservations
SET
quantity = \'' . $quantity . '\'
WHERE
item_id = \'' . $item_id . '\'
;';
$result = $db->query($updateQuery);
} else {
$insertQuery = '
INSERT INTO
ecmreservations
SET
date_modified = \'' . date("Y-m-d H:i:s") . '\',
date_entered = \'' . date("Y-m-d H:i:s") . '\',
modified_user_id = \'' . $_SESSION['authenticated_user_id'] . '\',
created_by = \'' . $_SESSION['authenticated_user_id'] . '\',
id = \'' . create_guid() . '\',
doc_id = \'' . $doc_id . '\',
item_id = \'' . $item_id . '\',
product_id = \'' . $product_id . '\',
quantity = \'' . $quantity . '\',
stock_id = \'' . $stock_id . '\',
doc_type = \'' . $doc_type . '\'
;';
$result = $db->query($insertQuery);
}
return $result;
}
function updateReservation($doc_id, $item_id, $quantity) {
global $db;
if ($quantity < 0) {
$q = $db->fetchByAssoc($db->query("SELECT quantity FROM ecmreservations WHERE doc_id='$doc_id' and item_id='$item_id'"));
if (!$q || $q['quantity']=='') return;
if ($q['quantity'] + $quantity <= 0)
$db->query("DELETE from ecmreservations WHERE doc_id='$doc_id' AND item_id='$item_id'");
else
$db->query("UPDATE ecmreservations SET quantity=quantity+$quantity WHERE doc_id='$doc_id' AND item_id='$item_id'");
}
/*
else {
$q = $db->fetchByAssoc($db->query("SELECT quantity FROM ecmreservations WHERE doc_id='$doc_id' and item_id='$item_id'"));
if (!$q || $q['quantity']='') {
}
}
*/
}
/**
* Save document reservations.
*/
function saveDocumentReservations($temp_doc_id) {
global $db;
$query =
'
UPDATE
`ecmreservations` AS r
SET
r.`session_id` = NULL,
r.`doc_id` = r.`temp_doc_id`,
r.`item_id` = r.`temp_item_id`,
r.`temp_doc_id` = NULL,
r.`temp_item_id` = NULL
WHERE
r.`temp_doc_id` = \'' . $temp_doc_id . '\'
;';
echo $query;
$result = $db->query($query);
return $result;
}
function removeReservation($temp_item_id) {
global $db;
$db->query("DELETE FROM ecmreservations WHERE temp_item_id='" . $temp_item_id . "'");
}
function removeDocumentReservations($doc_id) {
global $db;
return $db->query("DELETE FROM ecmreservations WHERE doc_id='" . $doc_id . "'");
}
function removeTempDocumentReservations($temp_doc_id) {
global $db;
return $db->query("DELETE FROM ecmreservations WHERE temp_doc_id='" . $temp_doc_id . "'");
}
function removeSaleProductReservations($item_id) {
global $db;
$db->query("DELETE FROM ecmreservations WHERE item_id='" . $item_id . "'");
}
function getProductReservations($ecmsale_id, $item_id) {
global $db;
$res = $db->fetchByAssoc($db->query("SELECT quantity as res FROM ecmreservations WHERE doc_id='" . $ecmsale_id . "' AND item_id='" . $item_id . "'"));
return $res['res'];
}
function getStockR($product_id, $stock_id = "", $item_id = "") {
global $db;
if ($stock_id!='')
$stock = " and stock_id='" . $stock_id . "'";
else
$stock = "";
$z = "select sum(quantity) as q from ecmreservations where product_id='" . $product_id . "'" . $stock;
$q = $db->query($z);
$row = $db->fetchByAssoc($q);
return (int) $row['q'];
}
//stock without reservations
function getRealStock($product_id, $stock_id ="") {
global $db;
if ($stock_id!='')
$stock = " and stock_id='" . $stock_id . "'";
else
$stock = "";
$rquery = "SELECT sum(quantity) as quantity FROM ecmstockstates WHERE product_id='" . $product_id ."'". $stock;
//get stockstates quantity
$r = $db->fetchByAssoc($db->query($rquery));
if (!$r['quantity'])
$r['quantity'] = 0;
return $r['quantity'];
}
function getStock($product_id, $stock_id = "",$part_id="") {
global $db;
if ($stock_id!='')
$stock = " and stock_id='" . $stock_id . "'";
else
$stock = "";
if ($part_id!='')
$part = " and part_id='" . $part_id . "'";
else
$part = "and part_id is null";
$rquery = "SELECT quantity FROM ecmstockstates WHERE product_id='" . $product_id ."'". $stock." ".$part;
//get stockstates quantity
$r = $db->fetchByAssoc($db->query($rquery));
$rrquery = "SELECT sum(quantity) as res FROM ecmreservations WHERE product_id='" . $product_id . "'". $stock;
//get reservations
$rr = $db->fetchByAssoc($db->query($rrquery));
if (!is_array($rr))
$rr['res'] = 0;
return $r['quantity'] - $rr['res'];
}
function getStockArray($product_id) {
if (!$product_id) return '';
global $db;
$res = $db->query("SELECT id, name FROM ecmstocks WHERE deleted='0'");
$ret = array();
while ($row = $db->fetchByAssoc($res)) {
$qty = $this->getStock($product_id, $row['id']);
if ($qty> 0)
$ret[$row['name']] = $qty;
}
return $ret;
}
function getAllQuantity($product_id) {
global $db;
$r = $db->fetchByAssoc($db->query("
SELECT
CASE WHEN (SELECT sum(quantity) FROM ecmreservations WHERE ecmproduct_id='$product_id') IS NULL
THEN
(SELECT sum(quantity) FROM ecmstockstates WHERE product_id='$product_id')
ELSE
((SELECT sum(quantity) FROM ecmstockstates WHERE product_id='$product_id')-(SELECT sum(quantity) FROM ecmreservations WHERE ecmproduct_id='$product_id'))
END
as qty
"));
return $r['qty'];
}
function checkStock($p, $stock_id) {
global $db;
$p = base64_decode($p);
print_r($_REQUEST);
require_once('include/json_config.php');
$json_config = new json_config();
$json = getJSONobj();
$pll = array();
$exp = explode("||||", $p);
foreach ($exp as $ep) {
if ($ep) {
$pll[] = $json->decode(htmlspecialchars_decode($ep));
}
}
$p = $pll;
foreach ($p as $rr) {
$products[] = array("product_name" => $rr['name'], "product_id" => $rr['id'], "quantity" => $rr['quantity'], "item_id" => $rr['item_id']);
}
//print_r($products);echo "mmmmmmmmmm";
$str.='
';
$str.='
Sprawdzanie stanów magazynowych ';
$str.='
Produkt Ilość Magazyn ';
if (count($products) > 0) {
foreach ($products as $p) {
if (!$p['product_id'])
continue;
$qty = self::getStock($p['product_id'], $stock_id, $p['item_id']);
$str.='' . $p['product_name'] . ' ' . $p['quantity'] . ' ' . $qty . ' ';
if ($p['quantity'] > $qty) {
$no = true;
$str.='' . "błąd" . ' ';
}
else
$str.='' . "ok" . ' ';
$str.=' ';
}
}
$str.='
';
if ($no) {
$str.='
' . "Brakuje produktów na magazynie" . ' ';
$str.='
';
} else {
$str.='
';
}
$str.='
';
return $str;
}
function checkStockSales($p, $stock_id) {
global $db;
require_once('include/json_config.php');
$json_config = new json_config();
$json = getJSONobj();
$pll = array();
$exp = explode("||||", $p);
foreach ($exp as $ep) {
if ($ep) {
$pll[] = $json->decode(htmlspecialchars_decode($ep));
}
}
$p = $pll;
foreach ($p as $rr) {
$products[] = array("product_name" => $rr['name'], "product_id" => $rr['id'], "quantity" => $rr['quantity'], "reservation" => $rr['reservation'], "rq" => $rr['rq']);
}
$str.='';
$str.='
Sprawdzanie stanów magazynowych ';
$str.='
Produkt Ilość Ilość do rezerwacji Magazyn ';
if (count($products) > 0) {
foreach ($products as $p) {
if (!$p['product_id'] || !$p['reservation'])
continue;
$qty = self::getStock($p['product_id'], $stock_id);
$str.='' . $p['product_name'] . ' ' . $p['quantity'] . ' ' . $p['rq'] . ' ' . $qty . ' ';
if ($p['rq'] > $qty || $p['rq'] > $p['quantity']) {
$no = true;
$str.='' . "błąd" . ' ';
}
else
$str.='' . "ok" . ' ';
$str.=' ';
}
}
$str.='
';
if ($no) {
$str.='
' . "Brakuje produktów na magazynie lub wpisałeś za dużą ilość produktów do zarezerwowania" . ' ';
$str.='
';
} else {
$str.='
';
}
$str.='
';
return $str;
}
}
?>