2025-05-12 15:44:39 +00:00
< ? php
if ( ! defined ( 'sugarEntry' ) || ! sugarEntry )
die ( 'Not A Valid Entry Point' );
/**
* ***************************************************************************
* The contents of this file are subject to the RECIPROCAL PUBLIC LICENSE
* Version 1.1 ( " License " ); You may not use this file except in compliance
* with the License .
* You may obtain a copy of the License at
* http :// opensource . org / licenses / rpl . php . Software distributed under the
* License is distributed on an " AS IS " basis , WITHOUT WARRANTY OF ANY KIND ,
* either express or implied .
*
* You may :
* a ) Use and distribute this code exactly as you received without payment or
* a royalty or other fee .
* b ) Create extensions for this code , provided that you make the extensions
* publicly available and document your modifications clearly .
* c ) Charge for a fee for warranty or support or for accepting liability
* obligations for your customers .
*
* You may NOT :
* a ) Charge for the use of the original code or extensions , including in
* electronic distribution models , such as ASP ( Application Service
* Provider ) .
* b ) Charge for the original source code or your extensions other than a
* nominal fee to cover distribution costs where such distribution
* involves PHYSICAL media .
* c ) Modify or delete any pre - existing copyright notices , change notices ,
* or License text in the Licensed Software
* d ) Assert any patent claims against the Licensor or Contributors , or
* which would in any way restrict the ability of any third party to use the
* Licensed Software .
*
* You must :
* a ) Document any modifications you make to this code including the nature of
* the change , the authors of the change , and the date of the change .
* b ) Make the source code for any extensions you deploy available via an
* Electronic Distribution Mechanism such as FTP or HTTP download .
* c ) Notify the licensor of the availability of source code to your extensions
* and include instructions on how to acquire the source code and updates .
* d ) Grant Licensor a world - wide , non - exclusive , royalty - free license to use ,
* reproduce , perform , modify , sublicense , and distribute your extensions .
*
* The Original Code is : CommuniCore
* Olavo Farias
* 2006 - 04 - 7 olavo . farias @ gmail . com
*
* The Initial Developer of the Original Code is CommuniCore .
* Portions created by CommuniCore are Copyright ( C ) 2005 CommuniCore Ltda
* All Rights Reserved .
* ******************************************************************************
*/
require_once ( 'data/SugarBean.php' );
require_once ( 'include/utils.php' );
class EcmStockDocCorrect extends SugarBean {
var $field_name_map = array ();
// STANDARD FIELDS
var $id ;
var $date_entered ;
var $date_modified ;
var $modified_user_id ;
var $assigned_user_id ;
var $name ;
var $description ;
var $number ;
var $document_no ;
var $register_date ;
var $total ;
var $position_list ;
var $created_by ;
var $created_by_name ;
var $modified_by_name ;
var $assigned_user_name ;
var $stock_id ;
2025-05-29 17:43:29 +02:00
var $ecommerce_invoices ;
var $pdf_text ;
var $type ;
2025-05-12 15:44:39 +00:00
// SUBPANELS RELATED
// MODULE OBJECT DETAILS
var $module_dir = " EcmStockDocCorrects " ;
var $table_name = " ecmstockdoccorrects " ;
var $object_name = " EcmStockDocCorrect " ;
// RELATED TABLE NAMES
// USED TO RETRIEVE RELATED FIELDS FROM FORM POSTS.
var $additional_column_fields = Array (
'assigned_user_name' ,
'assigned_user_id' ,
'modified_user_id' ,
'created_by'
);
// RELATIONSHIP FIELDS
var $relationship_fields = Array (
'note_id' => 'notes'
);
function EcmStockDocCorrect () {
parent :: SugarBean ();
$this -> setupCustomFields ( 'EcmStockDocCorrects' );
foreach ( $this -> field_defs as $field ) {
$this -> field_name_map [ $field [ 'name' ]] = $field ;
}
}
var $new_schema = true ;
function get_summary_text () {
return " $this->name " ;
}
function create_list_query ( $order_by , $where , $show_deleted = 0 ) {
// Fill in the assigned_user_name
$custom_join = $this -> custom_fields -> getJOIN ();
$query = " SELECT " ;
$query .= " ecmstockdoccorrects.*
, users . user_name as assigned_user_name " ;
if ( $custom_join ) {
$query .= $custom_join [ 'select' ];
}
$query .= " FROM ecmstockdoccorrects
LEFT JOIN users
ON ecmstockdoccorrects . 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 " ;
} else if ( $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 " ;
} else if ( $order_by != " " )
$query .= " ORDER BY $order_by " ;
else
$query .= " ORDER BY ecmstockdoccorrects.name " ;
return $query ;
}
function create_export_query ( $order_by , $where ) {
$custom_join = $this -> custom_fields -> getJOIN ();
$query = " SELECT
ecmstockdoccorrects .* ,
users . user_name assigned_user_name " ;
if ( $custom_join ) {
$query .= $custom_join [ 'select' ];
}
$query .= " FROM ecmstockdoccorrects " ;
$query .= " LEFT JOIN users
ON ecmstockdoccorrects . assigned_user_id = users . id " ;
if ( $custom_join ) {
$query .= $custom_join [ 'join' ];
}
$query .= " " ;
$where_auto = " ecmstockdoccorrects.deleted=0
" ;
if ( $where != " " )
$query .= " where $where AND " . $where_auto ;
else
$query .= " where " . $where_auto ;
if ( $order_by != " " )
$query .= " ORDER BY $order_by " ;
else
$query .= " ORDER BY ecmstockdoccorrects.name " ;
return $query ;
}
function fill_in_additional_list_fields () {
}
function fill_in_additional_detail_fields () {
// FILL IN THE ASSIGNED_USER_NAME
$this -> assigned_user_name = get_assigned_user_name ( $this -> assigned_user_id );
$this -> created_by_name = get_assigned_user_name ( $this -> created_by );
$this -> modified_by_name = get_assigned_user_name ( $this -> modified_user_id );
}
function get_list_view_data () {
global $current_language ;
$this -> fill_in_additional_detail_fields ();
$app_list_strings = return_app_list_strings_language ( $current_language );
$mod_strings = return_module_language ( $current_language , 'EcmStockDocCorrects' );
$the_array = parent :: get_list_view_data ();
// get stocks
if ( $this -> ecmproduct_id != '' ){
//add subpanel data
$tmp = $this -> getPriceFromDocument ( $this -> ecmproduct_id );
$the_array [ 'QUANTITY' ] = format_number ( $tmp [ 'quantity' ] );
$the_array [ 'PRICE_NETTO' ] = format_number ( $tmp [ 'price_netto' ] );
$the_array [ 'TOTAL_NETTO' ] = $tmp [ 'total_netto' ] ;
}
$this -> retrieve ( $this -> id );
$the_array [ 'DOCUMENT_AUTOINCREMENT' ] = date ( 'd.m.Y' , strtotime ( $this -> register_date ));
$the_array [ 'STOCK_NAME' ] = $this -> stock_name ;
$the_array [ 'NUMBER' ] = '<a href="index.php?module=EcmStockDocCorrects&action=DetailView&record=' . $this -> id . '&offset=1">' . $this -> document_no . '</a>' ;
// create image options
$op = '<a href="index.php?module=EcmStockDocCorrects&action=createPDF&to_pdf=1&record=' . $this -> id . '" target="new"/><img src="modules/EcmStockDocCorrects/images/pdf.gif"/></a>' ;
$the_array [ 'OPTIONS' ] = $op ;
return $the_array ;
}
function getPriceFromDocument ( $product ){
$zap = $this -> db -> query ( " select quantity,price,total from ecmstockdoccorrectitems where id=' " . $product . " ' " );
$dane = $this -> db -> fetchByAssoc ( $zap );
return $dane ;
}
function getStatusMenuInfo ( $inJSON = true ) {
global $app_list_strings ;
$arr = array ();
foreach ( $app_list_strings [ 'ecmstockdoccorrects_status_dom' ] as $key => $value ) {
$arr [ $key ] = array (
'enabled' => '<div><img src="modules/EcmStockDocCorrects/images/' . $key . '.gif" />' . $value . '</div>' ,
'disabled' => '<div><img src="modules/EcmStockDocCorrects/images/' . $key . '.gif" />' . $value . '</div>'
);
}
$json = getJSONobj ();
if ( $inJSON )
return str_replace ( '"' , '\"' , $json -> encode ( $arr ) );
else
return $arr ;
}
function create_new_list_query ( $order_by , $where , $filter = array (), $params = array (), $show_deleted = 0 , $join_type = '' , $return_array = false , $parentbean , $singleSelect = false ) {
echo '<script type="text/javascript" src="include/ECM/EcmPreviewPDF/EcmPreviewPDF.js"></script>' ;
echo '<script language="javascript"> var EcmStockDocCorrectsStatusMenu = ' . $this -> getStatusMenuInfo () . '; </script>' ;
return parent :: create_new_list_query ( $order_by , $where , $filter , $params , $show_deleted , $join_type , $return_array , $parentbean , $singleSelect );
}
static function CreateImgStatus ( $id , $status ) {
global $app_list_strings ;
return '<span id="statusListEcmStockDocCorrect_' . $id . '" style="display:inline;"><img src="modules/EcmStockDocCorrects/images/' . $status . '.gif" title="' . $app_list_strings [ 'ecmstockdoccorrects_status_dom' ] [ $status ] . '" /></span>' ;
}
/**
* BUILDS A GENERIC SEARCH BASED ON THE QUERY STRING USING OR .
* DO NOT INCLUDE ANY $THIS -> BECAUSE THIS IS CALLED ON WITHOUT HAVING THE CLASS INSTANTIATED .
*/
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 , " ecmstockdoccorrects.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 , $simplemodule ) {
global $mod_strings , $app_list_strings ;
$xtpl -> assign ( " NAME " , $simplemodule -> name );
$xtpl -> assign ( " DESCRIPTION " , $simplemodule -> description );
return $xtpl ;
}
function bean_implements ( $interface ) {
switch ( $interface ) {
case 'ACL' :
return true ;
}
return false ;
}
function save ( $check_notify = FALSE ) {
global $current_user ;
// generate number
require_once ( 'modules/EcmSysInfos/EcmSysInfo.php' );
$EcmSysInfo = new EcmSysInfo ();
$template = $EcmSysInfo -> getFormatNumberForModule ( 'EcmStockDocCorrects' );
unset ( $EcmSysInfo );
if ( $template != " " ) {
if ( $template != " " ) {
require_once ( 'include/ECM/EcmDocumentNumberGenerator/EcmDocumentNumberGenerator.inc.php' );
$dng = new EcmDocumentNumberGenerator ( 'EcmStockDocCorrects' , $this -> register_date );
$this -> document_no = $dng -> parseNumber ( $template , $this -> stock_id );
}
}
$aa = explode ( '/' , $this -> document_no );
$this -> document_autoincrement = date ( " Ymd " , strtotime ( $this -> register_date )) . '' . sprintf ( '%03d' ,( string )(( int ) $aa [ 0 ]));
$return_id = parent :: save ( $check_notify );
$this -> savePositions ( $return_id );
// update total
$db = $GLOBALS [ 'db' ];
$vat_total = 0 ;
// add round total 21.08.2014 DB
if ( $this -> type != '6ed5b076-ddd1-9809-b236-54e5b2bcbd97' )
$r = $db -> fetchByAssoc ( $db -> query ( " SELECT sum(total) as sum FROM ecmstockdoccorrectitems WHERE ecmstockdoccorrect_id=' $this->id ' and deleted='0' " ) );
else
$r = $db -> fetchByAssoc ( $db -> query ( " SELECT sum(total_diff) as sum FROM ecmstockdoccorrectitems WHERE ecmstockdoccorrect_id=' $this->id ' and deleted='0' " ) );
$db -> query ( " UPDATE ecmstockdoccorrects SET total=' " . round ( $r [ 'sum' ], 2 ) . " ' WHERE id=' $this->id ' " );
return $return_id ;
}
// ***************************Start Managing Positions*************************//
function constructInsertQuery ( $data , $table = '' ) {
if ( $table == '' && isset ( $this -> object_name ) && $this -> object_name != '' ) {
$table = strtolower ( $this -> object_name ) . " items " ;
} else
return " " ;
$keys = array ();
$values = array ();
foreach ( $data as $key => $value ) {
$keys [] = $key ;
if ( is_array ( $value ))
$values [] = $value [ 1 ] . str_replace ( " ' " , " \ ' " , $value [ 0 ] ) . $value [ 1 ];
else
$values [] = " ' " . str_replace ( " ' " , " \ ' " , $value ) . " ' " ;
}
$q = " insert into ` $table ` (` " ;
$q .= implode ( '`, `' , $keys );
$q .= " `) values ( " ;
$q .= implode ( " , " , $values );
$q .= " ); " ;
$GLOBALS [ 'db' ] -> query ( " INSERT INTO log VALUES (' " . mysql_real_escape_string ( $q ) . " ') " );
return $q ;
}
function savePositions ( $id = null , $position_list = null ) {
global $current_user , $timedate ;
if ( $position_list == null )
$position_list = $this -> position_list ;
if ( $id == null )
$id = $this -> id ;
if ( count ( $position_list ) > 0 )
$this -> deleteAssignedPositions ( $id );
$arr = array ();
$position = 0 ;
for ( $i = 0 ; $i < count ( $position_list ); $i ++ ) {
if ( $position_list [ $i ] [ 'name' ] == '' )
continue ;
$p = $position_list [ $i ];
require_once ( " modules/EcmProducts/EcmProduct.php " );
$pr = new EcmProduct ();
$pr -> retrieve ( $p [ 'product_id' ] );
$p [ 'category_id' ] = $pr -> product_category_id ;
unset ( $pr );
$item_id = create_guid ();
$p [ 'id' ] = $p [ 'product_id' ];
$arr = array (
'id' => $item_id ,
'date_entered' => array (
'NOW()' ,
''
),
'date_modified' => array (
'NOW()' ,
''
),
'modified_user_id' => $current_user -> id ,
'assigned_user_id' => $current_user -> id ,
'created_by' => $current_user -> id ,
'deleted' => '0' ,
strtolower ( $this -> object_name ) . '_id' => $id ,
'ecmproduct_id' => $p [ 'product_id' ],
'position' => $position ,
'code' => $p [ 'product_code' ],
'name' => $p [ 'name' ],
'price' => (( empty ( $p [ 'price' ] ) || $p [ 'price' ] == '' ) ? 0 : $p [ 'price' ]),
'quantity' => (( empty ( $p [ 'quantity' ] ) || $p [ 'quantity' ] == '' ) ? 0 : $p [ 'quantity' ]),
'dd_unit_id' => $p [ 'unit_id' ],
'dd_unit_name' => $p [ 'unit_name' ],
'ecmproductcategory_id' => $p [ 'category_id' ],
'ecmstockoperation_id' => $p [ 'product_consignment_id' ],
'is_consignment' => $p [ 'product_is_consignment' ],
'old_price' => $p [ 'price_old' ],
);
$GLOBALS [ 'db' ] -> query ( " INSERT INTO log VALUES (' " . mysql_real_escape_string ( $query ) . " ') " );
$this -> db -> query ( $this -> constructInsertQuery ( $arr ) );
// stock operations
if ( $this -> type == '6ed5b076-ddd1-9809-b236-54e5b2bcbd97' ) { //discount
$p [ 'quantity' ] *= - 1 ;
$this -> deleteProductsFromStockWithConsignment ( $p , $id , $item_id ); //delete
$p [ 'quantity' ] *= - 1 ;
$this -> insertProductsToStock ( $p , $id , $this -> stock_id , $item_id ); // add
//calculate total
$GLOBALS [ 'db' ] -> query ( " UPDATE ecmstockdoccorrectitems SET old_total=ROUND(quantity*old_price,2) WHERE id = ' $item_id ' " );
$GLOBALS [ 'db' ] -> query ( " UPDATE ecmstockdoccorrectitems SET total_diff=total-old_total WHERE id = ' $item_id ' " );
} else { //normal operations
if ( $p [ 'quantity' ] < 0 ) {
if ( ! $p [ 'product_consignment_id' ] || $p [ 'product_consignment_id' ] == " " ){
$this -> deleteProductsFromStock ( $p , $id , $item_id );
} else {
$this -> deleteProductsFromStockWithConsignment ( $p , $id , $item_id );
}
} elseif ( $p [ 'quantity' ] > 0 )
$this -> insertProductsToStock ( $p , $id , $this -> stock_id , $item_id );
}
$position ++ ;
}
}
function insertProductsToStock ( $pl , $return_id , $stock_id , $itemid ) {
global $app_list_strings ;
$pp = 0 ;
$qq = 0 ;
$w = $GLOBALS [ 'db' ] -> query ( " select price,quantity from ecmstockoperations where deleted='0' and type='0' and stock_id=' " . $stock_id . " ' and product_id=' " . $pl [ 'id' ] . " ' ORDER BY counter ASC " );
while ( $rrr = $GLOBALS [ 'db' ] -> fetchByAssoc ( $w ) ) {
if ( $rrr [ 'quantity' ] > 0 ) {
$pp += ( float ) $rrr [ 'price' ] * ( int ) $rrr [ 'quantity' ];
$qq += ( int ) $rrr [ 'quantity' ];
}
}
if ( $_REQUEST [ 'so_id' ]) {
$pp = 0 ;
$qq = 0 ;
$r_cor_inv = $GLOBALS [ 'db' ] -> fetchByAssoc ( $GLOBALS [ 'db' ] -> query ( " select ecminvoiceout_id from ecminvoiceouts where id=' " . $_REQUEST [ 'so_id' ] . " ' " ) );
$r_inv = $GLOBALS [ 'db' ] -> fetchByAssoc ( $GLOBALS [ 'db' ] -> query ( " select wz_id from ecminvoiceouts where id=' " . $r_cor_inv [ 'ecminvoiceout_id' ] . " ' " ) );
$w_wz = $GLOBALS [ 'db' ] -> query ( " select price,quantity from ecmstockoperations where parent_id=' " . $r_inv [ 'wz_id' ] . " ' and product_id=' " . $pl [ 'id' ] . " ' ORDER BY counter ASC " );
while ( $r_wz = $GLOBALS [ 'db' ] -> fetchByAssoc ( $w_wz ) ) {
$pp += ( float ) $r_wz [ 'price' ] * ( float ) $r_wz [ 'quantity' ];
$qq += ( int ) $r_wz [ 'quantity' ];
}
if ( ! $pp ) {
$w_it = $GLOBALS [ 'db' ] -> query ( " select purchase_price,quantity from ecminvoiceoutitems where ecminvoiceout_id=' " . $r_cor_inv [ 'ecminvoiceout_id' ] . " ' and ecmproduct_id=' " . $pl [ 'id' ] . " ' " );
while ( $r_it = $GLOBALS [ 'db' ] -> fetchByAssoc ( $w_it ) ) {
$pp += ( float ) $r_it [ 'purchase_price' ] * ( float ) $r_it [ 'quantity' ];
$qq += ( int ) $r_it [ 'quantity' ];
}
}
}
require_once ( " modules/EcmStockOperations/EcmStockOperation.php " );
$o = new EcmStockOperation ();
require_once ( " modules/EcmProducts/EcmProduct.php " );
$o -> product_id = $pl [ 'id' ];
$p = new EcmProduct ();
$p -> retrieve ( $pl [ '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 ;
if ( $pl [ 'product_is_consignment' ] == '1' ){
$o -> part_no = $pl [ 'product_consignment_part' ];
} else {
if ( $p -> is_consignment == 1 ){
$o -> part_no = $this -> description ;
}
}
$o -> parent_id = $return_id ;
$r = $GLOBALS [ 'db' ] -> fetchByAssoc ( $GLOBALS [ 'db' ] -> query ( " select document_no from ecmstockdoccorrects where id=' " . $return_id . " ' " ) );
$o -> parent_name = $r [ 'document_no' ];
$o -> parent_type = " EcmStockDocCorrects " ;
$o -> quantity = round ( $pl [ 'quantity' ], $app_list_strings [ 'ecmproducts_unit_dom_precision' ][ $pl [ 'unit_id' ]]);
if ( $pl [ 'price' ])
$price = $pl [ 'price' ];
else
$price = $pp / $qq ;
$o -> price = $price ;
$o -> documentitem_id = $itemid ;
$o -> type = 0 ;
$oid = $o -> save ();
$GLOBALS [ 'db' ] -> query ( " update ecmstockoperations set price=' " . $price . " ' where id=' " . $oid . " ' " );
$GLOBALS [ 'db' ] -> query ( " update ecmstockdoccorrectitems set total=' " . ( $pl [ 'quantity' ] * $price ) . " ',price=' " . ( $price ) . " ' where id=' " . $itemid . " ' " );
require_once ( " modules/EcmStockStates/EcmStockState.php " );
$ss = new EcmStockState ();
$ss -> UpdateStockState ( $o -> stock_id , $o -> product_id );
}
function deleteProductsFromStockWithConsignment ( $p , $id , $itemid ) {
$p [ 'quantity' ] *= - 1 ;
$db = $GLOBALS [ 'db' ];
$in_p = $db -> fetchByAssoc ( $db -> query ( " SELECT price FROM ecmstockoperations WHERE id = ' " . $p [ 'product_consignment_id' ] . " ' " ) );
$this -> addProductToStockOut ( $p [ 'product_consignment_id' ], $p [ 'id' ], $p [ 'quantity' ], $in_p [ 'price' ], $this -> register_date , $id , $this -> stock_id , $itemid );
$db -> query ( " update ecmstockdoccorrectitems set total='- " . round ( $p [ 'quantity' ] * $in_p [ 'price' ], 2 ) . " ',price=' " . $in_p [ 'price' ] . " ' where id=' " . $itemid . " ' " );
}
function deleteProductsFromStock ( $p , $id , $itemid ) {
$p [ 'quantity' ] *= - 1 ;
$arr = array ();
$w = $GLOBALS [ 'db' ] -> query ( " select quantity as qty,id,price from ecmstockoperations where deleted='0' and type='0' and in_id IS NULL and stock_id=' " . $this -> stock_id . " ' and product_id=' " . $p [ 'id' ] . " ' ORDER BY counter ASC " );
$GLOBALS [ 'db' ] -> query ( " INSERT INTO log VALUES ('s: " . $this -> stock_id . " p: " . $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=' " . $this -> stock_id . " ' and product_id=' " . $p [ 'id' ] . " ' and deleted='0' ORDER BY counter ASC " );
$GLOBALS [ 'db' ] -> query ( " INSERT INTO log VALUES ('in: " . $rrr [ 'id' ] . " s: " . $this -> stock_id . " p: " . $p [ 'id' ] . " ') " );
$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 (( int ) $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 ;
foreach ( $rr as $r ) {
$qq += $r [ 'qty' ];
$pp += $r [ 'qty' ] * $r [ 'price' ];
$this -> addProductToStockOut ( $r [ 'id' ], $p [ 'id' ], $r [ 'qty' ], $r [ 'price' ], $this -> register_date , $id , $this -> stock_id , $itemid );
}
$GLOBALS [ 'db' ] -> query ( " update ecmstockdoccorrectitems set total='- " . $pp . " ',price=' " . ( $pp / $qq ) . " ' where id=' " . $itemid . " ' " );
}
function addProductToStockOut ( $stock_in_id , $product_id , $quantity , $price , $date , $correct_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 = $correct_id ;
$r = $GLOBALS [ 'db' ] -> fetchByAssoc ( $GLOBALS [ 'db' ] -> query ( " select document_no from ecmstockdoccorrects where id=' " . $correct_id . " ' " ) );
$o -> parent_name = $r [ 'document_no' ];
$o -> parent_type = " EcmStockDocCorrects " ;
$o -> quantity = $quantity ;
$o -> price = format_number ( $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 );
}
function getPositionListOperations ( $array = false )
{
$return_array = array ();
global $timedate , $app_list_strings ;
$z = $this -> db -> query ( " select o.in_id,o.product_id,o.product_code,o.product_name as name,o.quantity,o.price,sum(o.price*o.quantity) as total,p.unit_id, ci.ecmstockoperation_id
from ecmstockoperations o
inner join ecmproducts p on p . id = o . product_id
inner join ecmstockdoccorrectitems as ci on o . documentitem_id = ci . id
where o . parent_id = '".$this->id."' group by o . id " );
$pos = 0 ;
while ( $dane = $this -> db -> fetchByAssoc ( $z )){
$dane [ 'position' ] = $pos ;
$dane [ 'unit_name' ] = $app_list_strings [ 'ecmproducts_unit_dom' ][ $dane [ 'unit_id' ]];
$dane [ 'unit_precision' ] = $app_list_strings [ 'ecmproducts_unit_dom_precision' ][ $dane [ 'unit_id' ]];
$dane [ 'product_precision' ] = $app_list_strings [ 'ecmproducts_unit_dom_precision' ][ $dane [ 'unit_id' ]];
// Consignment
$dane [ 'product_consignment_id' ] = $dane [ 'ecmstockoperation_id' ];
// get Consignment Document no
$documents_map = array (
'EcmStockDocIns' => '' ,
'EcmStockDocCorrects' => 'KS' ,
'EcmStockDocMoves' => 'MM' ,
'EcmStockDocInsideIns' => 'RW'
);
$db = $GLOBALS [ 'db' ];
$doc = $db -> fetchByAssoc ( $db -> query ( " SELECT parent_type, parent_id FROM ecmstockoperations WHERE id=' " . $dane [ 'in_id' ] . " ' " ) );
$doc2 = $db -> fetchByAssoc ( $db -> query ( " SELECT document_no FROM " . strtolower ( $doc [ 'parent_type' ]) . " WHERE id=' " . $doc [ 'parent_id' ] . " ' " ) );
$dane [ 'product_consignment_doc' ] = $documents_map [ $doc [ 'parent_type' ]] . ' ' . $doc2 [ 'document_no' ];
$return_array [] = $dane ;
$pos ++ ;
}
return $return_array ;
}
function getPosition ( $position ) {
if ( ! is_array ( $position ))
return '' ;
global $timedate , $app_list_strings ;
$return_array = array ();
$return_array [ 'product_id' ] = $position [ 'ecmproduct_id' ];
$return_array [ 'record_id' ] = $position [ 'id' ];
$return_array [ 'position' ] = $position [ 'position' ];
$return_array [ 'product_code' ] = $position [ 'code' ];
$return_array [ 'name' ] = $position [ 'name' ];
$return_array [ 'quantity' ] = $position [ 'quantity' ];
$return_array [ 'price' ] = $position [ 'price' ];
$return_array [ 'total' ] = $position [ 'total' ];
$return_array [ 'old_total' ] = $position [ 'old_total' ];
$return_array [ 'old_price' ] = $position [ 'old_price' ];
$return_array [ 'total_diff' ] = $position [ 'total_diff' ];
$return_array [ 'unit_id' ] = $position [ 'dd_unit_id' ];
$return_array [ 'unit_name' ] = $position [ 'dd_unit_name' ];
if ( $return_array [ 'unit_name' ] == '' ) {
$lv = return_app_list_strings_language ( 'pl_pl' );
$return_array [ 'unit_name' ] = $lv [ 'ecmproducts_unit_dom' ] [ $return_array [ 'unit_id' ]];
}
$return_array [ 'unit_precision' ] = $app_list_strings [ 'ecmproducts_unit_dom_precision' ] [ $position [ 'dd_unit_id' ]];
$return_array [ 'product_precision' ] = $app_list_strings [ 'ecmproducts_unit_dom_precision' ] [ $position [ 'dd_unit_id' ]];
// Consignment
$return_array [ 'product_consignment_id' ] = $position [ 'ecmstockoperation_id' ];
// get Consignment Document no
$documents_map = array (
'EcmStockDocIns' => 'PZ' ,
'EcmStockDocCorrects' => 'KS' ,
'EcmStockDocMoves' => 'MM' ,
'EcmStockDocInsideIns' => 'PW'
);
$db = $GLOBALS [ 'db' ];
$doc = $db -> fetchByAssoc ( $db -> query ( " SELECT parent_type, parent_name FROM ecmstockoperations WHERE id=' " . $position [ 'ecmstockoperation_id' ] . " ' " ) );
$return_array [ 'product_consignment_doc' ] = $documents_map [ $doc [ 'parent_type' ]] . ' ' . $doc [ 'parent_name' ];
return $return_array ;
}
function getPositionList ( $array = false ) {
if ( isset ( $this -> id ) && $this -> id != '' ) {
$query = " SELECT * FROM ` " . strtolower ( $this -> object_name ) . " items` WHERE ` " . strtolower ( $this -> object_name ) . " _id`=' " . $this -> id . " ' order by position asc " ;
$r = $this -> db -> query ( $query );
$return_array = array ();
if ( $r ) {
while ( $w = $this -> db -> fetchByAssoc ( $r ) ) {
$return_array [] = $this -> getPosition ( $w );
}
$json = getJSONobj ();
return $array ? $return_array : $json -> encode ( $return_array );
}
}
return $array ? false : '[]' ;
}
function deleteAssignedPositions () {
if ( isset ( $this -> id ) && $this -> id != '' ) {
$query = " DELETE FROM ` " . strtolower ( $this -> object_name ) . " items` WHERE ` " . strtolower ( $this -> object_name ) . " _id`=' " . $this -> id . " ' " ;
$r = $this -> db -> query ( $query );
if ( $r )
return true ;
}
return false ;
}
function formatPositions ( $position_list ) {
return ;
if ( ! is_array ( $position_list ) || count ( $position_list ) == 0 )
return false ;
global $app_list_strings ;
require_once ( 'modules/Currencies/Currency.php' );
foreach ( $position_list as $key => $value ) {
$position_list [ $key ] [ 'unit_id' ] = $app_list_strings [ 'ecmproducts_unit_dom' ] [ $value [ 'unit_id' ]];
$position_list [ $key ] [ 'category_id' ] = $app_list_strings [ 'ecmproducts_category_dom' ] [ $value [ 'category_id' ]];
// $position_list[$key]['vat_id'] = format_number($value['vat_id']).'%';
$position_list [ $key ] [ 'discount' ] = format_number ( $value [ 'discount' ] ) . '%' ;
if ( isset ( $currency ) && ( $currency -> id == $this -> currency_id )) {
} else {
$currency = new Currency ();
$currency -> retrieve ( $this -> currency_id );
}
$position_list [ $key ] [ 'price' ] = format_number ( $value [ 'price' ] ) . ' ' . $currency -> iso4217 ;
$position_list [ $key ] [ 'total' ] = format_number ( $value [ 'total' ] ) . ' ' . $currency -> iso4217 ;
// $position_list[$key]['price'] = format_number($value['price']).' PLN';
// $position_list[$key]['total'] = format_number($value['total']).' PLN';
$this -> currency_symbol = $currency -> iso4217 ;
}
return $position_list ;
}
// ***************************End Managing Positions*************************//
// begin stock
// end stock
function ACLAccess ( $view , $is_owner = 'not_set' ) {
global $current_user ;
$file = 'modules/EcmGroupSales/EcmGroupSale.php' ;
if ( file_exists ( $file )) {
require_once ( $file );
$cc = EcmGroupSale :: loadSettings ( true );
} else
return true ;
if ( is_admin ( $current_user )) {
switch ( $view ) {
case 'confirming' :
return ( $this -> status == " s20 " );
case 'send_to_confirm' :
return ( $this -> status == " s10 " );
case 'pdf_toolbar' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " );
}
return true ;
}
if ( $is_owner == 'not_set' ) {
$is_owner = $this -> isOwner ( $current_user -> id );
}
// if we don't implent acls return true
if ( ! $this -> bean_implements ( 'ACL' ))
return true ;
$view = strtolower ( $view );
if ( $cc [ 'user_manager_role' ]) { // Manager
switch ( $view ) {
case 'list' :
case 'index' :
case 'listview' :
return ACLController :: checkAccess ( $this -> module_dir , 'list' , true );
case 'edit' :
case 'save' :
case 'popupeditview' :
case 'editview' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'edit' , $is_owner ));
case 'view' :
case 'detail' :
case 'detailview' :
return ACLController :: checkAccess ( $this -> module_dir , 'view' , $is_owner );
case 'delete' :
return ACLController :: checkAccess ( $this -> module_dir , 'delete' , $is_owner );
case 'export' :
return ACLController :: checkAccess ( $this -> module_dir , 'export' , $is_owner );
case 'import' :
return ACLController :: checkAccess ( $this -> module_dir , 'import' , true );
case 'send_to_confirm' :
return ( $this -> status == " s10 " );
case 'can_commit' :
return true ;
case 'confirming' :
return ( $this -> status == " s20 " );
case 'auto_commiting' :
return true ;
case 'pdf_toolbar' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " );
}
} else if ( $cc [ 'user_representative_extra_role' ]) { // Representative Extra
switch ( $view ) {
case 'list' :
case 'index' :
case 'listview' :
return ACLController :: checkAccess ( $this -> module_dir , 'list' , true );
case 'edit' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'edit' , $is_owner ) && ( $this -> status == " s10 " || $this -> status == " s40 " ));
case 'save' :
case 'popupeditview' :
case 'editview' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'edit' , $is_owner ));
case 'view' :
case 'detail' :
case 'detailview' :
return ACLController :: checkAccess ( $this -> module_dir , 'view' , $is_owner );
case 'delete' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'delete' , $is_owner ) && $this -> status == " s10 " );
case 'export' :
return ACLController :: checkAccess ( $this -> module_dir , 'export' , $is_owner );
case 'import' :
return ACLController :: checkAccess ( $this -> module_dir , 'import' , true );
case 'quote_to_sale' :
case 'quote_to_invoice' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " && $this -> status != " s60 " );
case 'send_to_confirm' :
return ( $this -> status == " s10 " );
case 'send_email' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " );
case 'can_commit' :
return true ;
case 'confirming' :
return false ;
case 'auto_commiting' :
return true ;
case 'pdf_toolbar' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " );
}
} else if ( $cc [ 'user_representative_role' ]) { // Representative
switch ( $view ) {
case 'list' :
case 'index' :
case 'listview' :
return ACLController :: checkAccess ( $this -> module_dir , 'list' , true );
case 'edit' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'edit' , $is_owner ) && ( $this -> status == " s10 " || $this -> status == " s40 " ));
case 'save' :
case 'popupeditview' :
case 'editview' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'edit' , $is_owner ));
case 'view' :
case 'detail' :
case 'detailview' :
return ACLController :: checkAccess ( $this -> module_dir , 'view' , $is_owner );
case 'delete' :
return ( ACLController :: checkAccess ( $this -> module_dir , 'delete' , $is_owner ) && $this -> status == " s10 " );
case 'export' :
return ACLController :: checkAccess ( $this -> module_dir , 'export' , $is_owner );
case 'import' :
return ACLController :: checkAccess ( $this -> module_dir , 'import' , true );
case 'quote_to_sale' :
case 'quote_to_invoice' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " && $this -> status != " s60 " );
case 'send_to_confirm' :
return ( $this -> status == " s10 " );
case 'send_email' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " );
case 'can_commit' :
return false ;
case 'confirming' :
return false ;
case 'auto_commiting' :
return false ;
case 'pdf_toolbar' :
return ( $this -> status != " s10 " && $this -> status != " s20 " && $this -> status != " s40 " );
}
}
// if it is not one of the above views then it should be implemented on the page level
return true ;
}
// mz generate number
function generateNumber ( $date ) {
$db = $GLOBALS [ 'db' ];
$r = $db -> fetchByAssoc ( $db -> query ( " SELECT count(id) as c FROM ecmstockdocinsideins WHERE deleted='0' AND YEAR(register_date)=' " . date ( 'Y' , strtotime ( $date ) ) . " ' " ) );
if ( ! $r || ! $r [ 'c' ] || $r [ 'c' ] == '' )
$count = 0 ;
else
$count = intval ( $r [ 'c' ] );
$count ++ ;
// has 5 digits?
$tmp = strval ( $count );
while ( strlen ( $tmp ) < 5 )
$tmp = '0' . $tmp ;
return date ( 'Ymd' , strtotime ( $date ) ) . $tmp ;
}
function generateDocNumberStock ( $stock , $date ) {
$db = $GLOBALS [ 'db' ];
$r = $db -> fetchByAssoc ( $db -> query ( " SELECT count(id) as c FROM ecmstockdoccorrects WHERE deleted='0' AND YEAR(register_date)=' " . date ( 'Y' , strtotime ( $date ) ) . " ' AND stock_id=' " . $stock . " ' " ) );
$r2 = $db -> fetchByAssoc ( $db -> query ( " SELECT no FROM ecmstocks WHERE id=' " . $stock . " ' " ) );
if ( ! $r || ! $r [ 'c' ] || $r [ 'c' ] == '' )
$count = 0 ;
else
$count = intval ( $r [ 'c' ] );
$count ++ ;
$tmp = strval ( $count );
while ( strlen ( $tmp ) < 5 )
$tmp = '0' . $tmp ;
$number = date ( " Ymd " , strtotime ( $date ) ) . $tmp ;
$n = intval ( substr ( $number , 8 , 5 ) );
$y = substr ( $number , 0 , 4 );
// has 3 digits?
while ( strlen ( $n ) < 3 )
$n = '0' . $n ;
if ( $r2 [ 'no' ] != '' )
return '' . $n . '/' . $r2 [ 'no' ] . '/' . $y ;
else
return '' . $n . '/' . $y ;
}
function formatNumber ( $number ) {
$n = intval ( substr ( $number , 8 , 5 ) );
$y = substr ( $number , 2 , 2 );
// has 3 digits?
while ( strlen ( $n ) < 3 )
$n = '0' . $n ;
return 'KS ' . $n . '/' . $y ;
}
}
?>