853 lines
32 KiB
PHP
853 lines
32 KiB
PHP
|
|
<?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 EcmNewKpkw 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;
|
||
|
|
// SUBPANELS RELATED
|
||
|
|
// MODULE OBJECT DETAILS
|
||
|
|
var $module_dir = "EcmNewKpkws";
|
||
|
|
var $table_name = "ecmnewkpkws";
|
||
|
|
var $object_name = "EcmNewKpkw";
|
||
|
|
// 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 EcmNewKpkw() {
|
||
|
|
parent::SugarBean ();
|
||
|
|
$this->setupCustomFields ( 'EcmNewKpkws' );
|
||
|
|
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 .= "ecmnewkpkws.*
|
||
|
|
,users.user_name as assigned_user_name";
|
||
|
|
if ($custom_join) {
|
||
|
|
$query .= $custom_join ['select'];
|
||
|
|
}
|
||
|
|
$query .= " FROM ecmnewkpkws
|
||
|
|
LEFT JOIN users
|
||
|
|
ON ecmnewkpkws.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 ecmnewkpkws.name";
|
||
|
|
return $query;
|
||
|
|
}
|
||
|
|
function create_export_query($order_by, $where) {
|
||
|
|
$custom_join = $this->custom_fields->getJOIN ();
|
||
|
|
$query = "SELECT
|
||
|
|
ecmnewkpkws.*,
|
||
|
|
users.user_name assigned_user_name";
|
||
|
|
if ($custom_join) {
|
||
|
|
$query .= $custom_join ['select'];
|
||
|
|
}
|
||
|
|
$query .= " FROM ecmnewkpkws ";
|
||
|
|
$query .= " LEFT JOIN users
|
||
|
|
ON ecmnewkpkws.assigned_user_id=users.id";
|
||
|
|
if ($custom_join) {
|
||
|
|
$query .= $custom_join ['join'];
|
||
|
|
}
|
||
|
|
$query .= "";
|
||
|
|
$where_auto = " ecmnewkpkws.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 ecmnewkpkws.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, 'EcmNewKpkws' );
|
||
|
|
$the_array = parent::get_list_view_data ();
|
||
|
|
//get stocks
|
||
|
|
$this->retrieve($this->id);
|
||
|
|
$the_array['CASH_NAME'] = $this->cash_name;
|
||
|
|
$the_array['AMOUNT']=number_format($this->amount, 2, ',', '.');
|
||
|
|
$the_array['NUMBER']='<a href="index.php?module=EcmNewKpkws&action=DetailView&record='.$this->id.'">'.$this->document_no.'</a>';
|
||
|
|
$the_array['PARENT_NAME']='<a href="index.php?module=Accounts&action=DetailView&record='.$this->parent_id.'">'.$this->parent_name.'</a>';
|
||
|
|
//create image options
|
||
|
|
$op = '<a href="index.php?module=EcmNewKpkws&action=createPDF&to_pdf=1&record='.$this->id.'" target="new"/><img src="modules/EcmNewKpkws/images/pdf.gif"/></a>';
|
||
|
|
$the_array['OPTIONS'] = $op;
|
||
|
|
|
||
|
|
return $the_array;
|
||
|
|
}
|
||
|
|
function getStatusMenuInfo($inJSON = true) {
|
||
|
|
global $app_list_strings;
|
||
|
|
$arr = array ();
|
||
|
|
foreach ( $app_list_strings ['ecmnewkpkws_status_dom'] as $key => $value ) {
|
||
|
|
$arr [$key] = array (
|
||
|
|
'enabled' => '<div><img src="modules/EcmNewKpkws/images/' . $key . '.gif" />' . $value . '</div>',
|
||
|
|
'disabled' => '<div><img src="modules/EcmNewKpkws/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 type="text/javascript" src="modules/EcmNewKpkws/ListView.js"></script>';
|
||
|
|
echo '<link rel="stylesheet" type="text/css" href="modules/EcmNewKpkws/ListView.css" />';
|
||
|
|
echo '<script language="javascript"> var EcmNewKpkwsStatusMenu = ' . $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="statusListEcmNewKpkw_' . $id . '" style="display:inline;"><img src="modules/EcmNewKpkws/images/' . $status . '.gif" title="' . $app_list_strings ['ecmnewkpkws_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, "ecmnewkpkws.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 saveRelations($id,$res){
|
||
|
|
$t=new EcmTransaction();
|
||
|
|
$total=$this->amount;
|
||
|
|
while($dane=$this->db->fetchByAssoc($res)){
|
||
|
|
if($total-$dane['value']>0){
|
||
|
|
$dane['value']=$dane['value'];
|
||
|
|
} else {
|
||
|
|
$dane['value']=$total;
|
||
|
|
}
|
||
|
|
if($this->type_id==0){
|
||
|
|
$t->createRelation($dane['ecmtransaction_id'],$dane['value'],$id);
|
||
|
|
} else {
|
||
|
|
$t->createRelationM($id,$dane['value'],$dane['ecmtransaction_id']);
|
||
|
|
}
|
||
|
|
$total=$total-$dane['value'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$id'");
|
||
|
|
}
|
||
|
|
function compareTransaction($id,$res){
|
||
|
|
$total=$this->amount;
|
||
|
|
$tmp=array();
|
||
|
|
|
||
|
|
echo '<pre>';
|
||
|
|
while($dane=$this->db->fetchByAssoc($res)){
|
||
|
|
|
||
|
|
if($total-$dane['value']>0){
|
||
|
|
$dane['value']=$dane['value'];
|
||
|
|
} else {
|
||
|
|
$dane['value']=$total;
|
||
|
|
}
|
||
|
|
if($this->type_id==0){
|
||
|
|
|
||
|
|
// $t->createRelation($dane['ecmtransaction_id'],$dane['value'],$id);
|
||
|
|
} else {
|
||
|
|
// $t->createRelationM($id,$dane['value'],$dane['ecmtransaction_id']);
|
||
|
|
}
|
||
|
|
$tmp[]=$dane;
|
||
|
|
$total=$total-$dane['value'];
|
||
|
|
echo $total;
|
||
|
|
}
|
||
|
|
|
||
|
|
var_dump($tmp);
|
||
|
|
}
|
||
|
|
function save($check_notify = FALSE) {
|
||
|
|
global $current_user;
|
||
|
|
|
||
|
|
$return_id = parent::save ( $check_notify );
|
||
|
|
$this->savePositions($return_id);
|
||
|
|
$this->id=$return_id;
|
||
|
|
|
||
|
|
$a=$this->db->query("select id,value,ecmtransaction_id from ecmnewkpkwitems where ecmnewkpkw_id='".$this->id."' order by value asc");
|
||
|
|
|
||
|
|
if($this->savet==1){
|
||
|
|
if($a->num_rows>0){
|
||
|
|
|
||
|
|
global $app_list_strings,$current_user;
|
||
|
|
$id=create_guid();
|
||
|
|
|
||
|
|
|
||
|
|
$this->db->query("insert into ecmtransactions set id='".$id."',
|
||
|
|
name='".strtoupper($app_list_strings['ecmkpkw_type_dir'][$this->type_id])." ".$this->document_no."',
|
||
|
|
date_entered=NOW(), date_modified=NOW(),created_by='".$current_user->id."',
|
||
|
|
description='".$this->description."',deleted=0,assigned_user_id='".$this->assigned_user_id."',value='".$this->amount."',
|
||
|
|
parent_name='".$this->parent_name."',parent_id='".$this->parent_id."',payment_date='".$this->register_date."'
|
||
|
|
,type='".$this->changeTypetoSave()."',paid='0',
|
||
|
|
register_date='".$this->register_date."',record_id='".$this->id."',record_type='EcmNewKpkws',type3='2',currency_id='".$this->currency_id."',
|
||
|
|
currency_value='".$this->currency_value."',settled='0'");
|
||
|
|
|
||
|
|
$this->saveRelations($id,$a);
|
||
|
|
|
||
|
|
} else {
|
||
|
|
global $app_list_strings,$current_user;
|
||
|
|
|
||
|
|
$this->db->query("insert into ecmtransactions set id='".create_guid()."',
|
||
|
|
name='".strtoupper($app_list_strings['ecmkpkw_type_dir'][$this->type_id])." ".$this->document_no."',
|
||
|
|
date_entered=NOW(), date_modified=NOW(),created_by='".$current_user->id."',
|
||
|
|
description='".$this->description."',deleted=0,assigned_user_id='".$this->assigned_user_id."',value='".$this->amount."',
|
||
|
|
parent_name='".$this->parent_name."',parent_id='".$this->parent_id."',payment_date='".$this->register_date."'
|
||
|
|
,type='".$this->changeTypetoSave()."',paid='0',
|
||
|
|
register_date='".$this->register_date."',record_id='".$this->id."',record_type='EcmNewKpkws',type3='2',currency_id='".$this->currency_id."',
|
||
|
|
currency_value='".$this->currency_value."',settled='0'");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $return_id;
|
||
|
|
}
|
||
|
|
/*
|
||
|
|
function createKpKwRelations($con){
|
||
|
|
while($row=$this->db->fetchByAssoc($con)){
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
*/
|
||
|
|
function changeTypetoSave(){
|
||
|
|
if($this->type_id==0)return 1;
|
||
|
|
if($this->type_id==1)return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
function calculateTotals($id){
|
||
|
|
$vats_summary='';
|
||
|
|
$total_netto=0;
|
||
|
|
$total_brutto=0;
|
||
|
|
$total_vat=0;
|
||
|
|
|
||
|
|
$res= $GLOBALS ['db']->query ("select ROUND(sum(total_netto),2) AS total_netto,ROUND(sum((total_netto*ecmvat_value)/100),2) AS total_vat,
|
||
|
|
ROUND(sum(((total_netto*ecmvat_value)/100)+total_netto),2) AS total_brutto,ecmvat_value
|
||
|
|
from ecmnewkpkwitems where ecmnewkpkw_id='".$id."' group by ecmvat_id");
|
||
|
|
while($cc = $GLOBALS ['db']->fetchByAssoc ($res)){
|
||
|
|
$total_netto+=$cc['total_netto'];
|
||
|
|
$total_brutto+=$cc['total_brutto'];
|
||
|
|
$total_vat+=$cc['total_vat'];
|
||
|
|
$vats_summary.=$cc['ecmvat_value'].'%:'.$cc['total_netto'].':'.$cc['total_vat'].':'.$cc['total_brutto'].',';
|
||
|
|
}
|
||
|
|
$res= $GLOBALS ['db']->query ("update ecmnewkpkws set vats_summary='".$vats_summary."',total_netto='".$total_netto."',total_vat='".$total_vat."',
|
||
|
|
total_brutto='".$total_brutto."' where id='".$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 = 1; $i <= count ( $position_list ); $i ++) {
|
||
|
|
if ($position_list [$i] ['name'] == '')
|
||
|
|
continue;
|
||
|
|
$p = $position_list [$i];
|
||
|
|
|
||
|
|
$item_id = create_guid();
|
||
|
|
$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,
|
||
|
|
'position' => $position,
|
||
|
|
'name' => $p ['name'],
|
||
|
|
'value' => $p ['value'],
|
||
|
|
'type' => $p ['type_id'],
|
||
|
|
'ecmtransaction_id' => $p ['id'],
|
||
|
|
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->db->query ( $this->constructInsertQuery ( $arr ) );
|
||
|
|
//add to stock
|
||
|
|
// $this->addProducts($arr,$id,$item_id);
|
||
|
|
$position ++;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
function addProducts($p,$id,$itemid){
|
||
|
|
$arr=array();
|
||
|
|
$w=$GLOBALS['db']->query("select quantity as qty,id,price from ecmcashoperations where deleted='0' and type='0' and in_id IS NULL and stock_id='".$this->stock_id."' and product_id='".$p['ecmproduct_id']."' and used='0'");
|
||
|
|
while($rrr=$GLOBALS['db']->fetchByAssoc($w)){
|
||
|
|
$ww=$GLOBALS['db']->query("select quantity from ecmcashoperations where in_id='".$rrr['id']."' and type='1' and in_id IS NOT NULL and stock_id='".$this->stock_id."' and product_id='".$p['ecmproduct_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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$avg_pq=0;
|
||
|
|
$avg_q=0;
|
||
|
|
foreach($rr as $r){
|
||
|
|
$this->addProductToStockOut($r['id'],$p['ecmproduct_id'],$r['qty'],$r['price'],$this->register_date,$id,$this->stock_id,$itemid);
|
||
|
|
$avg_pq+=$r['qty']*$r['price'];
|
||
|
|
$avg_q+=$r['qty'];
|
||
|
|
}
|
||
|
|
$GLOBALS['db']->query("update ecmnewkpkwitems set price='".($avg_pq/$avg_q)."',total='".$avg_pq."' where id='".$itemid."'");
|
||
|
|
}
|
||
|
|
function addProductToStockOut($stock_in_id,$product_id,$quantity,$price,$date,$mm_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=$mm_id;
|
||
|
|
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select document_no from ecmnewkpkws where id='".$mm_id."'"));
|
||
|
|
$o->parent_name=$r['document_no'];
|
||
|
|
$o->parent_type="EcmNewKpkws";
|
||
|
|
|
||
|
|
$o->quantity=$quantity;
|
||
|
|
$o->price=$price;
|
||
|
|
$o->in_id=$stock_in_id;
|
||
|
|
$o->documentitem_id=$itemid;
|
||
|
|
$o->type=1;
|
||
|
|
$oid=$o->save();
|
||
|
|
$GLOBALS['db']->query("update ecmcashoperations set price='".$price."' where id='".$oid."'");
|
||
|
|
|
||
|
|
require_once("modules/EcmStockStates/EcmStockState.php");
|
||
|
|
$ss=new EcmStockState();
|
||
|
|
$ss->UpdateStockState($o->stock_id,$o->product_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
function getPosition($position) {
|
||
|
|
if (! is_array ( $position ))
|
||
|
|
return '';
|
||
|
|
global $timedate,$app_list_strings;
|
||
|
|
$return_array = array ();
|
||
|
|
$return_array ['position'] = $position ['position'];
|
||
|
|
$return_array ['name'] = $position ['name'];
|
||
|
|
$return_array ['type'] = $app_list_strings['transaction_type_dom'][$position ['type']];
|
||
|
|
|
||
|
|
|
||
|
|
$return_array ['value'] = $position ['value'];
|
||
|
|
$r=$this->db->query("select value from ecmtransactions_rel where ecmtransaction_a_id='".$position ['ecmtransaction_id']."'");
|
||
|
|
if($r->num_rows>0){
|
||
|
|
$a=$this->db->fetchByAssoc($r);
|
||
|
|
$return_array ['value']=$a['value'];
|
||
|
|
}
|
||
|
|
$return_array ['ecmtransaction_id'] = $position ['ecmtransaction_id'];
|
||
|
|
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 ShowTransactionsTable(){
|
||
|
|
$arr=$this->getPositionList(true);
|
||
|
|
|
||
|
|
global $mod_strings;
|
||
|
|
if(count($arr)>0){
|
||
|
|
$table='
|
||
|
|
<table cellpadding="0" cellspacing="0" border="0" class="list view" style="width:50%">
|
||
|
|
<tr class="oddListRowS1">
|
||
|
|
<th style="text-align: center;"><b>'.$mod_strings['LBL_TR_FIELD_1'].'</b></th>
|
||
|
|
<th style="text-align: center;"><b>'.$mod_strings['LBL_TR_FIELD_2'].'</b></th>
|
||
|
|
<th style="text-align: center;"><b>'.$mod_strings['LBL_TR_FIELD_3'].'</b></th>
|
||
|
|
</tr>
|
||
|
|
';
|
||
|
|
$i == 0;
|
||
|
|
foreach($arr as $a){
|
||
|
|
$i++;
|
||
|
|
$table.='
|
||
|
|
<tr class="oddListRowS1">
|
||
|
|
<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;"><a href="index.php?module=EcmTransactions&action=DetailView&record='.$a['ecmtransaction_id'].'">'.$a['name'].'</a></td>
|
||
|
|
|
||
|
|
<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;text-align:right;">'.$a['type'].'</td>
|
||
|
|
<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;text-align:right;"><b>'.number_format($a['value'], 2, ',', '.').'</b></td>
|
||
|
|
|
||
|
|
';
|
||
|
|
}
|
||
|
|
$table.='</table><div style="width:50%;">
|
||
|
|
<table cellpadding="0" cellspacing="0" border="0" class="list view" style="width:25%" align="right">
|
||
|
|
<tr><th>'.$mod_strings['LBL_TR_TOTAL'].'<th><td style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;text-align:right;"><b>'.number_format($this->total_transactions, 2, ',', '.').'</b></td></tr>
|
||
|
|
</table></div>';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $table;
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
function insertProductsToStock($pl, $return_id, $stock_id, $itemid) {
|
||
|
|
require_once("modules/EcmStockOperations/EcmStockOperation.php");
|
||
|
|
$o = new EcmStockOperation();
|
||
|
|
|
||
|
|
require_once("modules/EcmProducts/EcmProduct.php");
|
||
|
|
$o->product_id = $pl['ecmproduct_id'];
|
||
|
|
$p = new EcmProduct();
|
||
|
|
$p->retrieve($pl['ecmproduct_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 = $return_id;
|
||
|
|
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select document_no from ecmnewkpkws where id='" . $return_id . "'"));
|
||
|
|
$o->parent_name = $r['document_no'];
|
||
|
|
$o->parent_type = "EcmNewKpkws";
|
||
|
|
|
||
|
|
//$GLOBALS['db']->query("INSERT INTO log VALUES ('".$pl['quantity']."')");
|
||
|
|
$o->quantity = format_number($pl['quantity']);
|
||
|
|
$o->price = str_replace(",", ".", str_replace(".", "", $pl['price']));
|
||
|
|
$o->documentitem_id = $itemid;
|
||
|
|
$o->type = 0;
|
||
|
|
$oid = $o->save();
|
||
|
|
$GLOBALS['db']->query("update ecmcashoperations set price='" . $pl['price'] . "' where id='" . $oid . "'");
|
||
|
|
|
||
|
|
require_once("modules/EcmStockStates/EcmStockState.php");
|
||
|
|
$ss = new EcmStockState();
|
||
|
|
$ss->UpdateStockState($o->stock_id, $o->product_id);
|
||
|
|
}
|
||
|
|
//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($cash_id,$type,$date) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$r = $db->fetchByAssoc($db->query("SELECT count(id) as c FROM ecmnewkpkws WHERE deleted='0' AND YEAR(register_date)='".date('Y',strtotime($date))."' and cash_id='".$cash_id."' and type_id='".$type."'"));
|
||
|
|
|
||
|
|
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 generateDocNumber($cash_id,$type,$date) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$r = $db->fetchByAssoc($db->query("SELECT count(id) as c FROM ecmnewkpkws WHERE deleted='0' AND YEAR(register_date)='".date('Y',strtotime($date)) ."' and cash_id='".$cash_id."' and type_id='".$type."'"));
|
||
|
|
$st = $db->fetchByAssoc($db->query("select no from ecmcashs where id='".$cash_id."'"));
|
||
|
|
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;
|
||
|
|
|
||
|
|
$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($st['no']!=''){
|
||
|
|
return ''.$n.'/'.$st['no'].'/'.$y;
|
||
|
|
} else {
|
||
|
|
return ''.$n.'/'.$y;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function formatNumber($number) {
|
||
|
|
$n = intval(substr($number, 8,5));
|
||
|
|
$y = substr($number,0,4);
|
||
|
|
|
||
|
|
//has 3 digits?
|
||
|
|
while (strlen($n) < 3)
|
||
|
|
$n = '0'.$n;
|
||
|
|
|
||
|
|
return 'WZ '.$n.'/'.$y;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|