383 lines
14 KiB
PHP
383 lines
14 KiB
PHP
|
|
<?php
|
||
|
|
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
|
||
|
|
/*********************************************************************************
|
||
|
|
* SugarCRM is a customer relationship management program developed by
|
||
|
|
* SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
|
||
|
|
*
|
||
|
|
* This program is free software; you can redistribute it and/or modify it under
|
||
|
|
* the terms of the GNU General Public License version 3 as published by the
|
||
|
|
* Free Software Foundation with the addition of the following permission added
|
||
|
|
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
|
||
|
|
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
|
||
|
|
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
|
||
|
|
*
|
||
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
|
|
* details.
|
||
|
|
*
|
||
|
|
* You should have received a copy of the GNU General Public License along with
|
||
|
|
* this program; if not, see http://www.gnu.org/licenses or write to the Free
|
||
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||
|
|
* 02110-1301 USA.
|
||
|
|
*
|
||
|
|
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
|
||
|
|
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
|
||
|
|
*
|
||
|
|
* The interactive user interfaces in modified source and object code versions
|
||
|
|
* of this program must display Appropriate Legal Notices, as required under
|
||
|
|
* Section 5 of the GNU General Public License version 3.
|
||
|
|
*
|
||
|
|
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||
|
|
* these Appropriate Legal Notices must retain the display of the "Powered by
|
||
|
|
* SugarCRM" logo. If the display of the logo is not reasonably feasible for
|
||
|
|
* technical reasons, the Appropriate Legal Notices must display the words
|
||
|
|
* "Powered by SugarCRM".
|
||
|
|
********************************************************************************/
|
||
|
|
/*********************************************************************************
|
||
|
|
|
||
|
|
* Description: TODO: To be written.
|
||
|
|
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
|
||
|
|
* All Rights Reserved.
|
||
|
|
* Contributor(s): ______________________________________..
|
||
|
|
********************************************************************************/
|
||
|
|
|
||
|
|
require_once('data/SugarBean.php');
|
||
|
|
require_once('include/utils.php');
|
||
|
|
|
||
|
|
class EcmTransaction extends SugarBean {
|
||
|
|
var $field_name_map = array();
|
||
|
|
|
||
|
|
var $id;
|
||
|
|
var $date_entered;
|
||
|
|
var $date_modified;
|
||
|
|
var $modified_user_id;
|
||
|
|
var $assigned_user_id;
|
||
|
|
var $name;
|
||
|
|
var $value;
|
||
|
|
var $type;
|
||
|
|
var $record_id;
|
||
|
|
var $description;
|
||
|
|
var $parent_id;
|
||
|
|
var $link;
|
||
|
|
var $parent_name;
|
||
|
|
var $payment_date;
|
||
|
|
|
||
|
|
var $module_dir = 'EcmTransactions';
|
||
|
|
var $table_name = "ecmtransactions";
|
||
|
|
var $object_name = "EcmTransaction";
|
||
|
|
|
||
|
|
var $new_schema = true;
|
||
|
|
|
||
|
|
var $additional_column_fields = array('assigned_user_name', 'assigned_user_id');
|
||
|
|
|
||
|
|
function EcmTransaction() {
|
||
|
|
parent::SugarBean();
|
||
|
|
$this->setupCustomFields('EcmTransactions');
|
||
|
|
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.="ecmtransactions.*,users.user_name as assigned_user_name";
|
||
|
|
if($custom_join)$query.=$custom_join['select'];
|
||
|
|
$query.=" FROM ecmtransactions ";
|
||
|
|
$query.="LEFT JOIN users ON ecmtransactions.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 ecmtransactions.name";
|
||
|
|
|
||
|
|
return $query;
|
||
|
|
}
|
||
|
|
function create_export_query($order_by,$where){
|
||
|
|
$custom_join = $this->custom_fields->getJOIN();
|
||
|
|
$query ="SELECT ";
|
||
|
|
$query.="ecmtransactions.*,users.user_name as assigned_user_name";
|
||
|
|
if($custom_join)$query.=$custom_join['select'];
|
||
|
|
$query.=" FROM ecmtransactions ";
|
||
|
|
$query.="LEFT JOIN users ON ecmtransactions.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 ecmtransactions.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(){
|
||
|
|
|
||
|
|
global $current_language;
|
||
|
|
$the_array=parent::get_list_view_data();https://system.saas-systems.pl/index.php?module=EcmTransactions&action=Popup&query=true&from=EcmNewKpKws&parent_id_advanced=edd6f25a-cbbb-ccbd-825e-544f6008be33&parent_name_advanced=%22EKOMER%20-%20K.Wr%C3%B3blewska%22%20Sp%C3%B3%C5%82ka%20Jawna¤cy_id_advanced=PLN&settled_advanced=0&type_advanced=1&hide_clear_button=true&mode=MultiSelect&create=true&metadata=undefined
|
||
|
|
$app_list_strings=return_app_list_strings_language($current_language);
|
||
|
|
$mod_strings=return_module_language($current_language,'EcmTransactions');
|
||
|
|
|
||
|
|
$the_array['NAME']=(($this->name == "") ? "<em>blank</em>" : $this->name);
|
||
|
|
$the_array['ENCODED_NAME']=$this->name;
|
||
|
|
|
||
|
|
|
||
|
|
$a=new Account();
|
||
|
|
$b=new EcmTransaction;
|
||
|
|
$b->retrieve($this->id);
|
||
|
|
|
||
|
|
$a->retrieve($b->parent_id);
|
||
|
|
if($_REQUEST['module']=='Home' || $_REQUEST['action']=='Popup'){
|
||
|
|
$the_array ['VALUE']=$this->getRelations();
|
||
|
|
}
|
||
|
|
$the_array ['PARENT_NAME'] ='<a href="index.php?action=DetailView&module=Accounts&record='.$b->parent_id.'&offset=5&stamp=1398339446031915300">'.$a->name.'</a>';
|
||
|
|
$the_array ['OPTIONS']='<input type="checkbox" id="transaction_checkbox" value="'.$this->id.'">';
|
||
|
|
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,"ecmtransactions.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,$ecmtransaction)
|
||
|
|
{
|
||
|
|
global $mod_strings,$app_list_strings;
|
||
|
|
$xtpl->assign("ECMPAYMENT_SUBJECT",$ecmtransaction->name);
|
||
|
|
return $xtpl;
|
||
|
|
}
|
||
|
|
|
||
|
|
function bean_implements($interface){
|
||
|
|
switch($interface){
|
||
|
|
case 'ACL':return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
function RemoveRelations(){
|
||
|
|
if($this->id!=''){
|
||
|
|
$db=$GLOBALS['db'];
|
||
|
|
$zap=$db->query("select * from ecmtransactions_rel where (ecmtransaction_a_id='".$this->id."' OR ecmtransaction_b_id='".$this->id."')");
|
||
|
|
while($res=$db->fetchByAssoc($zap)){
|
||
|
|
|
||
|
|
$db->query("update ecmtransactions set settled=0 where id='".$res['ecmtransaction_a_id']."'");
|
||
|
|
$db->query("update ecmtransactions set settled=0 where id='".$res['ecmtransaction_b_id']."'");
|
||
|
|
$db->query("delete from ecmtransactions_rel where id='".$res['id']."'");
|
||
|
|
}
|
||
|
|
$db->query("update ecmtransactions set settled=0 where id='".$this->id."'");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function prepareDataCustomMa($smartyO,$curO){
|
||
|
|
$id= explode('|',$_REQUEST['toRelId']);
|
||
|
|
array_pop($id);
|
||
|
|
$tmp=new EcmTransaction();
|
||
|
|
$curO->description="Zapłata do: ";
|
||
|
|
foreach ($id as $v){
|
||
|
|
$tmp->retrieve($v);
|
||
|
|
$curO->description.=$tmp->name.', ';
|
||
|
|
}
|
||
|
|
unset($tmp);
|
||
|
|
|
||
|
|
$amount= explode('|',$_REQUEST['toRelVal']);
|
||
|
|
array_pop($amount);
|
||
|
|
$curO->value=0;
|
||
|
|
foreach ($amount as $v){
|
||
|
|
$curO->value+=$v;
|
||
|
|
}
|
||
|
|
|
||
|
|
$curO->value=$curO->value;
|
||
|
|
$curO->register_date=date("d.m.Y");
|
||
|
|
|
||
|
|
$smartyO->assign('TORELVAL',$_REQUEST['toRelVal']);
|
||
|
|
$smartyO->assign('TORELID',$_REQUEST['toRelId']);
|
||
|
|
}
|
||
|
|
function createRelationM($id, $value, $payment) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
global $current_user;
|
||
|
|
$db->query("
|
||
|
|
INSERT INTO ecmtransactions_rel VALUES (
|
||
|
|
'".create_guid()."',
|
||
|
|
'".date("Y-m-d H:i:s")."',
|
||
|
|
'".$current_user->id."',
|
||
|
|
'$id',
|
||
|
|
'$payment',
|
||
|
|
'$value'
|
||
|
|
)
|
||
|
|
");
|
||
|
|
|
||
|
|
//check is settled
|
||
|
|
//winien
|
||
|
|
$res = $db->fetchByAssoc($db->query("
|
||
|
|
SELECT (abs(t.value)-sum(rel.value)) as unsettled FROM ecmtransactions AS t
|
||
|
|
INNER JOIN ecmtransactions_rel AS rel
|
||
|
|
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
|
||
|
|
WHERE t.id='$id'
|
||
|
|
"));
|
||
|
|
echo "
|
||
|
|
SELECT (abs(t.value)-sum(rel.value)) as unsettled FROM ecmtransactions AS t
|
||
|
|
INNER JOIN ecmtransactions_rel AS rel
|
||
|
|
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
|
||
|
|
WHERE t.id='$id'
|
||
|
|
";
|
||
|
|
echo 'WINIEN '.$row['unsettled'].'...';
|
||
|
|
if ($res['unsettled']==0)
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$id'");
|
||
|
|
//ma
|
||
|
|
$res = $db->fetchByAssoc($db->query("
|
||
|
|
SELECT (abs(t.value)-sum(rel.value)) as unsettled FROM ecmtransactions AS t
|
||
|
|
INNER JOIN ecmtransactions_rel AS rel
|
||
|
|
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
|
||
|
|
WHERE t.id='$payment'
|
||
|
|
"));
|
||
|
|
echo 'MA '.$row['unsettled'].'...';
|
||
|
|
if ($res['unsettled']==0)
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$payment'");
|
||
|
|
echo "
|
||
|
|
SELECT (abs(t.value)-sum(rel.value)) as unsettled FROM ecmtransactions AS t
|
||
|
|
INNER JOIN ecmtransactions_rel AS rel
|
||
|
|
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
|
||
|
|
WHERE t.id='$payment'
|
||
|
|
";
|
||
|
|
echo '1';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function createRelation($id, $value, $payment) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
global $current_user;
|
||
|
|
$db->query("
|
||
|
|
INSERT INTO ecmtransactions_rel VALUES (
|
||
|
|
'".create_guid()."',
|
||
|
|
'".date("Y-m-d H:i:s")."',
|
||
|
|
'".$current_user->id."',
|
||
|
|
'$id',
|
||
|
|
'$payment',
|
||
|
|
'$value'
|
||
|
|
)
|
||
|
|
");
|
||
|
|
//check is settled
|
||
|
|
//winien
|
||
|
|
$res = $db->fetchByAssoc($db->query("
|
||
|
|
SELECT (t.value-sum(rel.value)) as unsettled FROM ecmtransactions AS t
|
||
|
|
INNER JOIN ecmtransactions_rel AS rel
|
||
|
|
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
|
||
|
|
WHERE t.id='$id'
|
||
|
|
"));
|
||
|
|
|
||
|
|
if ($res['unsettled']==0)
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$id'");
|
||
|
|
//ma
|
||
|
|
$res = $db->fetchByAssoc($db->query("
|
||
|
|
SELECT (t.value-sum(rel.value)) as unsettled FROM ecmtransactions AS t
|
||
|
|
INNER JOIN ecmtransactions_rel AS rel
|
||
|
|
ON (rel.ecmtransaction_a_id=t.id OR rel.ecmtransaction_b_id=t.id)
|
||
|
|
WHERE t.id='$payment'
|
||
|
|
"));
|
||
|
|
|
||
|
|
if ($res['unsettled']==0)
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$payment'");
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
function saveRelationsCustomMa($id){
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$tmp=$this->createArray();
|
||
|
|
for($i=0;count($tmp)>$i;$i++){
|
||
|
|
$this->createRelation($tmp[$i]['id'],$tmp[$i]['amount'],$id);
|
||
|
|
}
|
||
|
|
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$id'");
|
||
|
|
|
||
|
|
}
|
||
|
|
function saveRelationsCustomWinien($id){
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$tmp=$this->createArray();
|
||
|
|
for($i=0;count($tmp)>$i;$i++){
|
||
|
|
$this->createRelation($tmp[$i]['id'],$tmp[$i]['amount'],$id);
|
||
|
|
}
|
||
|
|
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='1' WHERE id='$id'");
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
function getRelations(){
|
||
|
|
$res=$this->db->query("select value from ecmtransactions_rel where ecmtransaction_a_id='".$this->id."' or ecmtransaction_b_id='".$this->id."'");
|
||
|
|
if($this->value<0){
|
||
|
|
$tmp=$this->value;
|
||
|
|
while ($data=$this->db->fetchByAssoc($res)){
|
||
|
|
$tmp+=abs($data['value']);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$tmp=$this->value;
|
||
|
|
while ($data=$this->db->fetchByAssoc($res)){
|
||
|
|
$tmp-=abs($data['value']);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $tmp;
|
||
|
|
}
|
||
|
|
|
||
|
|
function mergeArray($id,$amount){
|
||
|
|
$new=Array();
|
||
|
|
for($i=0;count($id)>$i;$i++){
|
||
|
|
$new[$i]['id']=$id[$i];
|
||
|
|
$new[$i]['amount']=$amount[$i];
|
||
|
|
}
|
||
|
|
return $new;
|
||
|
|
}
|
||
|
|
|
||
|
|
function createArray(){
|
||
|
|
$id= explode('|',$_REQUEST['toRelId']);
|
||
|
|
array_pop($id);
|
||
|
|
$amount= explode('|',$_REQUEST['toRelVal']);
|
||
|
|
array_pop($amount);
|
||
|
|
|
||
|
|
return $this->mergeArray($id,$amount);
|
||
|
|
}
|
||
|
|
|
||
|
|
function save($check_notify=FALSE){
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//$this->register_date = $this->payment_date;
|
||
|
|
|
||
|
|
$id=parent::save($check_notify);
|
||
|
|
|
||
|
|
mysql_query("delete from ecmtransactions_ecminvoiceouts where ecmtransaction_id='".$id."'");
|
||
|
|
for($i=0;$i<100;$i++){
|
||
|
|
if($_REQUEST['inv_id'.$i]){
|
||
|
|
mysql_query("insert into ecmtransactions_ecminvoiceouts(id,ecmtransaction_id,ecminvoiceout_id,date_entered,date_modified,deleted,created_by,modified_user_id) values('".create_guid()."','".$id."','".$_REQUEST['inv_id'.$i]."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','0','".$_SESSION['authenticated_user_id']."','".$_SESSION['authenticated_user_id']."')");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$db->query("UPDATE ecmtransactions SET settled='0' WHERE id='".$this->id."'");
|
||
|
|
if($_REQUEST['toRelVal']!='' && $_REQUEST['toRelId']!='' && $this->type=='1')$this->saveRelationsCustomMa($id);
|
||
|
|
if($_REQUEST['toRelVal']!='' && $_REQUEST['toRelId']!='' && $this->type=='0')$this->saveRelationsCustomWinien($id);
|
||
|
|
if($check_notify==false)header("Location: index.php?module=EcmPaymentStates&action=AccountPaymentStates&account_id=".$this->parent_id);
|
||
|
|
|
||
|
|
return $id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|