Files

521 lines
18 KiB
PHP
Raw Permalink Normal View History

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.
* ****************************************************************************** */
global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings, $current_user, $app_list_strings;
require_once('modules/EcmReceipts/EcmReceipt.php');
require_once('modules/EcmReceipts/Forms.php');
require_once('include/time.php');
require_once('include/json_config.php');
$file = 'modules/EcmGroupSales/EcmGroupSale.php';
if (file_exists($file)) {
$cc = array();
require_once($file);
$cc = EcmGroupSale::loadSettings();
}
$OPT = array();
$OPT['row_item_height'] = $cc['row_item_height'];
$OPT['row_item_height_selected'] = $cc['row_item_height_selected'];
$OPT['rows_on_item_list'] = $cc['rows_on_item_list'];
$OPT['position_table_height'] = $OPT['row_item_height'] * $OPT['rows_on_item_list'] + 40 + $OPT['rows_on_item_list'] * 4;
$OPT['quick_product_item_adding'] = $cc['quick_product_item_adding'];
if ($cc['checkbox_demo'] == 1) {
$query = 'SELECT COUNT(id) as count FROM ecmreceipts WHERE deleted = \'0\'';
$result = $GLOBALS['db']->query($query);
if (is_resource($result)) {
$row = $GLOBALS['db']->fetchByAssoc($result);
if (isset($row['count']) && is_numeric($row['count'] = intval($row['count'])) && $row['count'] >= 10) {
$OPT['checkbox_demo'] = 1;
}
}
}
$OPT['check_parent_id'] = true;
$cq = $current_user->getPreference('confirm_receipts');
$OPT['user']['confirm_receipts'] = ((isset($cq) && $cq) ? 1 : 0);
$focus = new EcmReceipt();
if (!$focus->id) {
$temp_id = create_guid();
} else {
$temp_id = $focus->id;
}
//for outside modules
if (isset($_REQUEST['out_module']) && $_REQUEST['out_module'] != '' && isset($_REQUEST['out_id']) && $_REQUEST['out_id'] != '') {
$focus->wz_id = $_REQUEST['out_id'];
$outModule = $_REQUEST['out_module'];
$outId = $_REQUEST['out_id'];
$OPT['fromOutside'] = true;
$path = 'modules/' . $outModule . '/LoadEcmReceipts.php';
if (file_exists($path)) {
require_once($path);
$_REQUEST['record'] = '';
$outside_create = true;
}
}
$pt = $pdid = null;
if (isset($_REQUEST['record']) && $_REQUEST['record'] != '') {
$focus->retrieve($_REQUEST['record']);
if (isset($focus->id) && $focus->id != '') {
if ($focus->accepted == 1) {
echo 'You cannot edit this receipt. This receipt is accepted. <a href="index.php?module=' . $_REQUEST['return_module'] . '&action=' . $_REQUEST['return_action'] . '&record=' . $_REQUEST['return_id'] . '">return</a>';
return;
}
$focus->format_all_fields();
// dump($focus->toArray());
// exit;
$focus->position_list = str_replace('&quot;', '\"', $focus->getPositionList());
//has wz?
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT ecmstockdocout_id as wz FROM ecmreceiptitems WHERE ecmreceipt_id='" . $focus->id . "'"));
if ($r['wz'] && $r['wz'] != '') {
$OPT['from_wz'] = true;
}
}
} elseif ($_REQUEST['parent_doc_type']=='EcmSales') {
session_start();
$positions = $_SESSION[$_REQUEST['temp_id']];
$sales = array();
foreach ($positions as $v)
$sales[] = $v['parent_doc_id'];
$OPT['new_number'] = true;
$OPT['parent_doc_type'] = 'EcmSales';
$temp_id = $_REQUEST['temp_id'];
//set accounts information
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT * FROM ecmsales WHERE id='" . $positions[0]['parent_doc_id'] . "'"));
$focus->payment_method = $r['payment_method'];
$focus->payment_date_d = $r['payment_deadline'];
$focus->parent_id = $r['parent_id'];
$focus->parent_name = $r['parent_name'];
$focus->parent_contact_name = $r['parent_contact_name'];
$focus->parent_contact_title = $r['parent_contact_title'];
$focus->parent_address_street = $r['parent_address_street'];
$focus->parent_address_city = $r['parent_address_city'];
$focus->parent_address_postalcode = $r['parent_address_postalcode'];
$focus->parent_address_country = $r['parent_address_country'];
$focus->order_no = $r['order_no'];
$focus->to_is_vat_free = $r['to_is_vat_free'];
$focus->contact_id = $r['contact_id'];
$focus->stock_id = $r['stock_id'];
$focus->to_nip = $r['to_vatid'];
$focus->currency_value = $r['currency_value'];
$focus->pdf_type = $r['invoice_type'];
$focus->currency_id = $r['currency_id'];
$focus->parent_shipping_address_name = $r['parent_shipping_address_name'];
$focus->parent_shipping_address_street = $r['parent_shipping_address_street'];
$focus->parent_shipping_address_city = $r['parent_shipping_address_city'];
$focus->parent_shipping_address_postalcode = $r['parent_shipping_address_postalcode'];
$focus->parent_shipping_address_country = $r['parent_shipping_address_country'];
//set possition list
//get prepayment informations
global $db;
$res = $db->query("SELECT document_no, inv_value, currency_id, currency_value FROM ecmprepaymentinvoices WHERE ecmsale_id IN ('".implode("','", $sales)."')");
$sum = 0;
while ($row = $db->fetchByAssoc($res)) {
$sum+=$row['inv_value'];
$focus->prepaid_nr.=$row['document_no'].' ';
//$focus->currency_id = $row['currency_id'];
//$focus->currency_value = $row['currency_value'];
}
//get older invoices prepaid
$old = $db->fetchByAssoc($db->query("SELECT sum(i.prepaid) as s FROM ecminvoiceouts AS i INNER JOIN ecminvoiceoutitems as ii ON i.id=ii.ecminvoiceout_id WHERE ii.parent_doc_id IN ('".implode("','", $sales)."')"));
$sum+=$old['s'];
$focus->prepaid=$sum;
//get parent currency
$cur = $db->fetchByAssoc($db->query("SELECT currency_id as cid FROM accounts WHERE id='".$focus->parent_id."'"));
$c = new Currency();
$c->retrieve($cur['cid']);
$focus->currency_id = $cur['cid'];
$focus->currency_value = $c->conversion_rate;
unset($c);
$pl = array();
foreach ($positions as $pos) {
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT * FROM ecmsaleitems WHERE id ='" . $pos['parent_doc_item_id'] . "'"));
$t = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT type FROM ecmproducts WHERE id='".$pos['id']."'"));
$position = array();
$position['iid'] = create_guid();
$position['code'] = $pos['code'];
$position['name'] = $pos['name'];
$position['id'] = $pos['id'];
$position['quantity'] = $pos['quantity'];
//calculate currency
if ($r['currency_id']==$focus->currency_id)
$position['startprice'] = $r['price'];
else {
$c = new Currency();
$c->retrieve($r['currency_id']);
if ($focus->currency_id=='PLN') {
$position['startprice'] = $r['price']*$c->conversion_rate;
} else {
$position['startprice'] = $r['price']/$focus->currency_value;
}
}
//$position['selling_price'] = $r['price'];
$position['discount'] = $r['discount'];
$position['total'] = $r['total'];
$position['unit_id'] = $r['dd_unit_id'];
$position['unit_name'] = $r['dd_unit_name'];
$position['vat_id'] = $r['ecmvat_id'];
$position['vat_name'] = $r['ecmvat_name'];
$position['vat_value'] = $r['ecmvat_value'];
$position['category_id'] = $r['ecmproductcategory_id'];
$position['currency_id'] = $r['currency_id'];
$position['currency_name'] = $r['currency_name'];
$position['recipient_code'] = $r['recipient_code'];
$position['type'] = $t['type'];
$position['parent_doc_id'] = $r['ecmsale_id'];
$position['parent_doc_type'] = 'EcmSales';
$position['parent_doc_item_id'] = $pos['parent_doc_item_id'];
$position['temp_item_id'] = create_guid();
$position['temp_date'] = date("Y-m-d H:i:s");
include_once("modules/EcmStockOperations/EcmStockOperation.php");
$op = new EcmStockOperation();
$position['stock'] = $op->getStock($r['ecmproduct_id'], $focus->stock_id);
$position['parent_doc_rq'] = $op->getStockR($r['ecmproduct_id'], $focus->stock_id);
$pl[] = $position;
}
$focus->position_list = str_replace('&quot;', '\"', $json->encode($pl));
}
else {
if (isset($_REQUEST['contact_id']) && $_REQUEST['contact_id'] != '' && isset($_REQUEST['contact_name']) && $_REQUEST['contact_name'] != '') {
$_REQUEST['parent_type'] = 'Contacts';
$_REQUEST['parent_name'] = $_REQUEST['contact_name'];
$_REQUEST['parent_id'] = $_REQUEST['contact_id'];
$OPT['check_parent_id'] = false;
}
if (isset($_REQUEST['account_id']) && $_REQUEST['account_id'] != '' && isset($_REQUEST['account_name']) && $_REQUEST['account_name'] != '') {
$_REQUEST['parent_type'] = 'Accounts';
$_REQUEST['parent_name'] = $_REQUEST['account_name'];
$_REQUEST['parent_id'] = $_REQUEST['account_id'];
$OPT['check_parent_id'] = false;
}
$OPT['new_number'] = true;
if (isset($cc)) {
//payment condition
$cc_list = EcmGroupSale::getPositionList('ecmpaymentconditions');
//$focus->ecmpaymentcondition_id = $cc['default_payment_condition'];
//$focus->ecmpaymentcondition_name = $cc_list[$cc['default_payment_condition']];
//default template
$focus->template_id = $cc['default_document_template'];
$fftemplateid = $focus->template_id;
}
}
if (isset($_REQUEST['isCorrect']) && $_REQUEST['isCorrect'] == 'true') {
$OPT['new_number'] = true;
$focus->type = 'correct';
$focus->name = '';
$focus->ecmreceipt_id = $focus->id;
$focus->id = '';
}
$OPT['old_status'] = (isset($focus->status) && $focus->status != '') ? $focus->status : 'not_accepted';
$OPT['invoice']['type'] = (isset($focus->type) && $focus->type != '') ? $focus->status : 'normal';
if ($_REQUEST['isDuplicate'] == 'true' || $outside_create == true) {
$_POST['isDuplicate'] = true;
$focus->id = '';
$OPT['isDuplicate'] = true;
$OPT['new_number'] = true;
}
if (!isset($focus->discount) || $focus->discount == '') {
$focus->discount = '0.00';
}
if ($OPT['new_number'] == true) {
$datef = $current_user->getPreference('datef');
if ($datef != '') {
$sugar_config['datef'];
}
$datef = 'd.m.Y';
//echo $datef;
$focus->register_date = date($datef);
$focus->payment_date = date($datef, mktime() + 30 * 24 * 60 * 60);
$focus->sell_date = date($datef);
}
if (isset($_REQUEST['out_module']) && $_REQUEST['out_module'] != '' && isset($_REQUEST['out_id']) && $_REQUEST['out_id'] != '') {
//todo dodać relację
$focus->wz_id = $_REQUEST['out_id'];
}
$tmp = $current_user->getPreference('num_grp_sep');
$tmp = ".";
if (!isset($tmp) || $tmp == '' || $tmp == NULL) {
$tmp = $sugar_config['default_number_grouping_seperator'];
}
$OPT['sep_1000'] = $tmp;
$tmp = $current_user->getPreference('dec_sep');
$tmp = ",";
if (!isset($tmp) || $tmp == '' || $tmp == NULL) {
$tmp = $sugar_config['default_decimal_seperator'];
}
$OPT['dec_sep'] = $tmp;
$tmp = $current_user->getPreference('default_currency_significant_digits');
if (!isset($tmp) || $tmp == '' || $tmp == NULL) {
$tmp = $sugar_config['default_currency_significant_digits'];
}
$OPT['dec_len'] = $tmp;
$OPT['default_unit'] = '1';
$OPT['default_vat'] = '23.00';
$OPT['default_category'] = '';
$OPT['default_currency'] = '-99';
$OPT['type'] = $focus->type;
$OPT['to_is_vat_free'] = $focus->to_is_vat_free;
require_once('modules/EcmTexts/EcmText.php');
foreach ($app_list_strings['ecmlanguages_dom'] as $key => $value) {
$data = EcmText::LoadText(null, null, 'EcmReceipts', $key);
//var_dump($data);
if (isset($data[0]) && isset($data[0]['data']))
$d = $data[0]['data']; else {
$d = $PDFLL;
if (!isset($d['labels'])) {
$d['labels'] = $PDFLL['labels'];
}
if (!isset($d['texts']['Contacts']['header_text'])) {
$d['texts']['Contacts']['header_text'] = $mod_strings['LBL_DEFAULT_CONTACT_HEADER_TEXT'];
}
if (!isset($d['texts']['Contacts']['footer_text'])) {
$d['texts']['Contacts']['footer_text'] = $mod_strings['LBL_DEFAULT_CONTACT_FOOTER_TEXT'];
}
if (!isset($d['texts']['Contacts']['ads_text'])) {
$d['texts']['Contacts']['ads_text'] = $mod_strings['LBL_DEFAULT_CONTACT_ADS_TEXT'];
}
if (!isset($d['texts']['Accounts']['header_text'])) {
$d['texts']['Accounts']['header_text'] = $mod_strings['LBL_DEFAULT_ACCOUNT_HEADER_TEXT'];
}
if (!isset($d['texts']['Accounts']['footer_text'])) {
$d['texts']['Accounts']['footer_text'] = $mod_strings['LBL_DEFAULT_ACCOUNT_FOOTER_TEXT'];
}
if (!isset($d['texts']['Accounts']['ads_text'])) {
$d['texts']['Accounts']['ads_text'] = $mod_strings['LBL_DEFAULT_ACCOUNT_ADS_TEXT'];
}
}
$OPT['ecmlanguage'][$key]['texts'] = $d['texts'];
}
$w = $GLOBALS[db]->query('select name,id,value from ecmvats where deleted=\'0\' order by name');
$nvats = mysql_num_rows($w);
while ($r = $GLOBALS[db]->fetchByAssoc($w)) {
$VAT[$r['id']] = array(
'id' => $r['id'],
'name' => $r['name'],
'value' => $r['value']
);
}
$show_pdf = $current_user->getPreference('show_pdf_in_div');
if (!isset($show_pdf)) {
require_once('modules/EcmGroupSales/EcmGroupSale.php');
$cc = EcmGroupSale::loadSettings();
$show_pdf = $cc['show_pdf_in_div_global'];
}
$json = getJSONobj();
$scriptOpt = '
<script language="javascript">
var SHOW_PDF_IN_DIV =' . $show_pdf . ';
var UNIT =' . str_replace('&quot;', '\"', $json->encode($GLOBALS['app_list_strings']['ecmproducts_unit_dom'])) . ';
var VAT = ' . str_replace('&quot;', '\"', $json->encode($VAT)) . ';
var OPT = ' . str_replace('&quot;', '\"', $json->encode($OPT)) . ';
var MOD = ' . str_replace('&quot;', '\"', $json->encode($mod_strings)) . ';
var N;
</script>';
$larr = array(
'en_us',
'pl_pl',
'ge_ge',
);
foreach ($larr as $la) {
$lv = return_app_list_strings_language($la);
$UNIT_LANG[$la] = $lv['ecmproducts_unit_dom'];
}
$scriptOpt .= '
<script language="javascript">
var UNIT_LANG=' . str_replace('&quot;', '\"', $json->encode($UNIT_LANG)) . ';
</script>';
echo $scriptOpt;
require_once('include/MVC/View/SugarView.php');
require_once('modules/EcmReceipts/view/EditView/view.edit.my.php');
$edit = new ViewEditMy();
$edit->ss = new Sugar_Smarty();
$edit->module = 'EcmReceipts';
$edit->bean = $focus;
$edit->tplFile = 'include/ECM/EcmViews/EditView/Tabs/EditView.tpl';
$stocks = '<option value="">' . $GLOBALS['app_list_strings']['stock_select'] . '</option>';
$w = $GLOBALS['db']->query('select name,id from ecmstocks where deleted = \'0\' order by name asc');
$selectedId = $focus->stock_id ? : $app_list_strings['ecmreceipts_stock_id_dom'];
while ($r = $GLOBALS['db']->fetchByAssoc($w)) {
$stocks.='<option value="' . $r['id'] . '"';
if ($r['id'] == $selectedId) {
$stocks.=' selected';
}
$stocks.='>' . $r['name'] . '</option>';
}
$edit->ss->assign('PARENT_TYPE', $pt ? : 'Accounts');
$edit->ss->assign('PARENT_DOC_ID', $pdid);
$edit->ss->assign('STOCK', $stocks);
$edit->ss->assign('OPT', $OPT);
$edit->ss->assign('TEMP_ID', $temp_id);
$edit->preDisplay();
if (isset($fftemplateid) && $fftemplateid != '') {
$focus->template_id = $fftemplateid;
}
$arr_template = $focus->getTemplateList();
if (isset($outside_create) && $outside_create == true) {
$edit->ss->assign('OUT_MODULE', $_REQUEST['out_module']);
$edit->ss->assign('OUT_ID', $_REQUEST['out_id']);
}
$arr_template = $focus->getTemplateList();
$tt = '';
foreach ($arr_template as $k => $v) {
$tt.='<option value="' . $k . '"';
if ($k == $focus->template_id) {
$tt.=' selected';
}
$tt.='>' . $v . '</option>';
}
$edit->ss->assign('DOCUMENT_TEMPLATES_OPTIONS', $tt);
$edit->ss->assign('POSITION_LIST', $focus->position_list);
$edit->ss->assign('MFP', $focus->loadParserArray());
echo $edit->display();