Files
crm.e5.pl/modules/EcmReturns/EcmReturn.php

899 lines
34 KiB
PHP
Raw Normal View History

2024-04-27 09:23:34 +02: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 EcmReturn 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;
// TABLE COLUMNS
var $number;
var $document_no;
var $parent_type;
var $parent_name;
var $parent_id;
var $type;
var $status;
var $register_date;
var $parent_address_street;
var $parent_address_city;
var $parent_address_postalcode;
var $parent_address_country;
var $total_netto;
var $total;
var $discount;
var $to_vatid;
var $ecmlanguage;
var $to_is_vat_free;
var $header_text;
var $footer_text;
var $ads_text;
var $template_id;
var $template_name;
var $email_id;
var $parent_name_copy;
var $parent_contact_name;
var $parent_contact_title;
var $ecmpaymentcondition_id;
var $ecmpaymentcondition_name;
var $ecmpaymentcondition_text;
var $ecmdeliverycondition_id;
var $ecmdeliverycondition_name;
var $ecmdeliverycondition_text;
var $validtill_date;
var $order_origin;
var $contact_id;
var $contact_name;
var $show_images_on_offers;
var $currency_symbol;
var $currency_id;
var $show_ean;
var $template;
var $pdf;
var $position_list;
var $parser;
var $account;
var $contact;
var $user;
var $mfp;
// added 4.03.2009
var $pricebook_id;
var $show_recipient_code;
// RELATED FIELDS
var $created_by;
var $created_by_name;
var $modified_by_name;
var $assigned_user_name;
// SUBPANELS RELATED
// MODULE OBJECT DETAILS
var $module_dir = "EcmReturns";
var $table_name = "ecmreturns";
var $object_name = "EcmReturn";
// 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',
'email_id' => 'emails'
// 'account_id' => 'account',
// 'contact_id' => 'contact',
);
function EcmReturn() {
parent::SugarBean ();
$this->setupCustomFields ( 'EcmReturns' );
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 .= "ecmreturns.*
,users.user_name as assigned_user_name";
if ($custom_join) {
$query .= $custom_join ['select'];
}
$query .= " FROM ecmreturns
LEFT JOIN users
ON ecmreturns.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 ecmreturns.name";
return $query;
}
function create_export_query($order_by, $where) {
$custom_join = $this->custom_fields->getJOIN ();
$query = "SELECT
ecmreturns.*,
users.user_name assigned_user_name";
if ($custom_join) {
$query .= $custom_join ['select'];
}
$query .= " FROM ecmreturns ";
$query .= " LEFT JOIN users
ON ecmreturns.assigned_user_id=users.id";
if ($custom_join) {
$query .= $custom_join ['join'];
}
$query .= "";
$where_auto = " ecmreturns.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 ecmreturns.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 );
/*
* $result = $this->db->query ( "SELECT status, parent_id, contact_id, template_id, assigned_user_id FROM ecmreturns WHERE id='$this->id'" ); if (is_resource ( $result )) { $row = $this->db->fetchByAssoc ( $result ); if ($row) { $this->status = $row ['status']; $this->parent_id = $row ['parent_id']; $this->contact_id = $row ['contact_id']; $this->template_id = $row ['template_id']; $this->assigned_user_id = $row ['assigned_user_id']; } }
*/
/*
* $query = "select `number`, `template_id` from `ecmreturns` where `id`='$this->id' limit 1"; $r = $this->db->query($query); if($r) { $r = $this->db->fetchByAssoc($r); $this->template_id = $r['template_id']; $this->number = $r['number']; $this->setTemplate(); $this->document_no = $this->formatNumber(); }
*/
}
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, 'EcmReturns' );
$the_array = parent::get_list_view_data ();
// THE NEW LISTVIEW CODE ONLY FETCHES COLUMNS THAT WE'RE DISPLAYING AND NOT ALL
// THE COLUMNS SO WE NEED THESE CHECKS.
$this->retrieve ( $this->id );
$the_array ['NAME'] = (($this->name == "") ? "<em>blank</em>" : $this->name);
$the_array ['TOTAL'] = format_number ( $this->total_netto );
// create image options
$op = '<a href="index.php?module=EcmReturns&action=createPDF&to_pdf=1&show_img=1&show_ean=0&show_ean2=0&show_recipient_code=0&record=' . $this->id . '" target="new"/><img src="modules/EcmReturns/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 ['ecmreturns_status_dom'] as $key => $value ) {
$arr [$key] = array (
'enabled' => '<div><img src="modules/EcmReturns/images/' . $key . '.gif" />' . $value . '</div>',
'disabled' => '<div><img src="modules/EcmReturns/images/' . $key . '.gif" />' . $value . '</div>'
);
}
$json = getJSONobj ();
if ($inJSON)
return str_replace ( '&quot;', '\"', $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/EcmReturns/ListView.js"></script>';
echo '<link rel="stylesheet" type="text/css" href="modules/EcmReturns/ListView.css" />';
echo '<script language="javascript"> var EcmReturnsStatusMenu = ' . $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="statusListEcmReturn_' . $id . '" style="display:inline;"><img src="modules/EcmReturns/images/' . $status . '.gif" title="' . $app_list_strings ['ecmreturns_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, "ecmreturns.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;
if (! $this->id || $this->id == '') {
// generate number
$this->number = $this->generateNumber ();
$this->document_no = $this->formatNumber ( $this->number );
}
$this->vats_summary = htmlspecialchars ( $this->vats_summary );
$return_id = parent::save ( $check_notify );
$this->savePositions ( $return_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 .= ");";
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];
if (! empty ( $p ['category_id'] ) || $p ['category_id'] == '') {
require_once ("modules/EcmProducts/EcmProduct.php");
$pr = new EcmProduct ();
$pr->retrieve ( $p ['id'] );
$p ['category_id'] = $pr->product_category_id;
}
$arr = array (
'id' => create_guid (),
'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'],
'quantity' => ((empty ( $p ['quantity'] ) || $p ['quantity'] == '') ? 0 : $p ['quantity']),
'price_start' => ((empty ( $p ['price_start'] ) || $p ['price_start'] == '') ? 0 : $p ['price_start']),
'total_netto' => ((empty ( $p ['total_netto'] ) || $p ['total_netto'] == '') ? 0 : $p ['total_netto']),
'dd_unit_id' => $p ['unit_id'],
'dd_unit_name' => $p ['unit_name'],
'ecmvat_id' => $p ['ecmvat_id'],
'ecmvat_name' => $p ['ecmvat_name'],
'ecmvat_value' => $p ['ecmvat_value'],
'ecmproductcategory_id' => $p ['category_id'],
'recipient_code' => $p ['recipient_code'],
'remarks' => $p ['remarks']
);
$this->db->query ( $this->constructInsertQuery ( $arr ) );
$position ++;
}
}
function getPosition($position) {
if (! is_array ( $position ))
return '';
global $timedate;
$return_array = array ();
$return_array ['product_id'] = $position ['ecmproduct_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_start'] = $position ['price_start'];
$return_array ['price_netto'] = $position ['price_netto'];
$return_array ['discount'] = $position ['discount'];
$return_array ['total_netto'] = $position ['total_netto'];
$return_array ['unit_id'] = $position ['dd_unit_id'];
$return_array ['unit_name'] = $position ['dd_unit_name'];
$return_array ['ecmvat_id'] = $position ['ecmvat_id'];
$return_array ['ecmvat_name'] = $position ['ecmvat_name'];
$return_array ['ecmvat_value'] = $position ['ecmvat_value'];
$return_array ['recipient_code'] = $position ['recipient_code'];
$return_array ['remarks'] = $position ['remarks'];
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*************************//
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;
}
function searchProductsToCorrectList($positions) {
if ($this->type == '1')
$search_price = true;
else
$search_price = false;
$account_id = $this->parent_id;
$db = $GLOBALS ['db'];
$can_confirm = true; // we hope everything goes well :)
$items = array (); // items on correct
$errors = array(); //not found products
foreach ( $positions as $pos ) {
$quantity = $pos ['quantity']; // qty we need
// search for invoice items
$q = "SELECT ii.* FROM ecminvoiceoutitems AS ii
INNER JOIN ecminvoiceouts AS i
ON ii.ecminvoiceout_id=i.id
WHERE i.type='normal'
AND ii.ecmproduct_id='" . $pos ['product_id'] . "'
AND i.parent_id='$account_id'
AND i.canceled='0'
AND i.deleted='0'
AND ii.deleted='0'
AND YEAR(i.register_date) > 2009 ";
if ($search_price)
$q .= "AND ii.price_netto='" . $pos ['price_start'] . "' ORDER BY i.register_date DESC";
else
$q .= "ORDER BY ii.price_netto DESC";
$res = $db->query ( $q );
if ($pos['product_id'] == '674c36eb-e614-3658-9c90-54466f1e382a')
echo $q.'<br>';
// loop throw items and search good quantity
while ( $row = $db->fetchByAssoc ( $res ) ) {
$tmp = array ();
$tmp ['old_ecminvoiceoutitem_id'] = $row ['id'];
// any before corrects?
$oi_q = "SELECT ii.id, ii.ecminvoiceout_id, ii.price_netto, ii.quantity FROM ecminvoiceoutitems as ii INNER JOIN ecminvoiceouts AS i ON i.id=ii.ecminvoiceout_id WHERE i.canceled='0' AND i.deleted='0' AND ii.ecminvoiceoutitem_id='" . $row ['id'] . "' order by ii.date_entered desc limit 0,1";
$oi = $db->fetchByAssoc ( $db->query ( $oi_q ) );
if (! is_null ( $oi ['id'] ) && $oi ['id'] != "") {
if ($search_price)
if ($io ['price_netto'] != $row ['price_netto'])
continue;
$row ['quantity'] = $oi ['quantity'];
$tmp ['old_ecminvoiceoutitem_id'] = $oi ['id'];
}
if ($row ['quantity'] == 0)
continue; // no product
if ($row ['quantity'] >= $quantity) {
// take all!
$tmp ['quantity'] = $row ['quantity'] - $quantity;
$tmp ['old_qty'] = $row ['quantity'];
$quantity = 0;
} else {
// take as many as U can!
$tmp ['quantity'] = 0;
$tmp ['old_qty'] = $row ['quantity'];
$quantity -= ($tmp ['old_qty'] - $tmp ['quantity']);
}
// fill item array
$tmp ['code'] = $row ['code'];
$tmp ['name'] = $row ['name'];
$tmp ['id'] = $row ['ecmproduct_id'];
$tmp ['startprice'] = $row ['startprice'];
$tmp ['discount'] = $row ['discount'];
$tmp ['price'] = $row ['price'];
$tmp ['price_netto'] = $row ['price_netto'];
$tmp ['total_netto'] = $tmp ['price_netto'] * $tmp ['quantity'];
$tmp ['total'] = $tmp ['price'] * $tmp ['quantity'];
$tmp ['vat_id'] = $row ['ecmvat_id'];
$tmp ['vat_name'] = $row ['ecmvat_name'];
$tmp ['vat_value'] = $row ['ecmvat_value'];
$tmp ['currency_id'] = $row ['currency_id'];
$tmp ['currency_name'] = $row ['currency_name'];
$tmp ['tax_code'] = $row ['tax_code'];
$tmp ['recipient_code'] = $row ['recipient_code'];
$tmp ['item_id'] = $row ['id'];
$tmp ['unit_id'] = $row ['dd_unit_id'];
$tmp ['unit_name'] = $app_list_strings ['ecmproducts_unit_dom'] [$row ['dd_unit_id']];
$tmp ['parent_doc_id'] = $this->id;
$tmp ['parent_doc_type'] = 'EcmReturn';
$tmp ['parent_doc_item_id'] = $pos ['id'];
$items [] = $tmp;
if ($quantity == 0)
break; // goto next product
}
unset ( $row );
unset ( $res );
if ($quantity > 0 && $search_price) {
echo 'Próbuje bez search price';
// try without search price
$q = "SELECT ii.* FROM ecminvoiceoutitems AS ii
INNER JOIN ecminvoiceouts AS i
ON ii.ecminvoiceout_id=i.id
WHERE i.type='normal'
AND ii.ecmproduct_id='" . $pos ['id'] . "'
AND i.parent_id='$account_id'
AND i.canceled='0'
AND i.deleted='0'
AND ii.deleted='0'
AND YEAR(i.register_date) > 2009
ORDER BY ii.price_netto DESC";
$res = $db->query ( $q );
// loop throw items and search good quantity
while ( $row = $db->fetchByAssoc ( $res ) ) {
$tmp = array ();
$tmp ['old_ecminvoiceoutitem_id'] = $row ['id'];
// any before corrects?
$oi_q = "SELECT ii.id, ii.ecminvoiceout_id, ii.price_netto, ii.quantity FROM ecminvoiceoutitems as ii INNER JOIN ecminvoiceouts AS i ON i.id=ii.ecminvoiceout_id WHERE i.canceled='0' AND i.deleted='0' AND ii.ecminvoiceoutitem_id='" . $row ['id'] . "' order by ii.date_entered desc limit 0,1";
$oi = $db->fetchByAssoc ( $db->query ( $oi_q ) );
if (! is_null ( $oi ['id'] ) && $oi ['id'] != "") {
$row ['quantity'] = $oi ['quantity'];
$tmp ['old_ecminvoiceoutitem_id'] = $oi ['id'];
}
if ($row ['quantity'] == 0)
continue; // no product
if ($row ['quantity'] >= $quantity) {
// take all!
$tmp ['quantity'] = $row ['quantity'] - $quantity;
$tmp ['old_qty'] = $row ['quantity'];
$quantity = 0;
} else {
// take as many as U can!
$tmp ['quantity'] = 0;
$tmp ['old_qty'] = $row ['quantity'];
$quantity -= ($tmp ['old_qty'] - $tmp ['quantity']);
}
// fill item array
$tmp ['code'] = $row ['code'];
$tmp ['name'] = $row ['name'];
$tmp ['id'] = $row ['ecmproduct_id'];
$tmp ['unit_id'] = $row ['dd_unit_id'];
$tmp ['unit_name'] = $app_list_strings ['ecmproducts_unit_dom'] [$row ['dd_unit_id']];
$tmp ['startprice'] = $row ['startprice'];
$tmp ['discount'] = $row ['discount'];
$tmp ['price_brutto'] = $row ['price_brutto'];
$tmp ['price_netto'] = $row ['price_netto'];
$tmp ['total_netto'] = $tmp ['price_netto'] * $tmp ['quantity'];
$tmp ['total'] = $tmp ['price'] * $tmp ['quantity'];
$tmp ['vat_id'] = $row ['ecmvat_id'];
$tmp ['vat_name'] = $row ['ecmvat_name'];
$tmp ['vat_value'] = $row ['ecmvat_value'];
$tmp ['currency_id'] = $row ['currency_id'];
$tmp ['currency_name'] = $row ['currency_name'];
$tmp ['tax_code'] = $row ['tax_code'];
$tmp ['recipient_code'] = $row ['recipient_code'];
$tmp ['item_id'] = $row ['id'];
$tmp ['unit_id'] = $row ['dd_unit_id'];
$tmp ['unit_name'] = $app_list_strings ['ecmproducts_unit_dom'] [$row ['dd_unit_id']];
$tmp ['parent_doc_id'] = $this->id;
$tmp ['parent_doc_type'] = 'EcmReturn';
$tmp ['parent_doc_item_id'] = $pos ['id'];
//can't search product with user price
$tmp['diffrent_price'] = true;
$items [] = $tmp;
if ($quantity == 0)
break; // goto next product
}
}
if ($quantity > 0) {
$errors[] = $pos ['product_code'];
}
}
foreach ( $items as $k => $i ) {
$inv = $db->fetchByAssoc ( $db->query ( "SELECT i.id, i.document_no FROM ecminvoiceoutitems AS ii
INNER JOIN ecminvoiceouts AS i
ON ii.ecminvoiceout_id = i.id
AND ii.id = '" . $i ['item_id'] . "'" ) );
$items [$k] ['inv_document_no'] = $inv ['document_no'];
$items [$k] ['inv_id'] = $inv ['id'];
if (! is_null ( $i ['old_ecminvoiceoutitem_id'] ) && $i ['old_ecminvoiceoutitem_id'] != "") {
$cor = $db->fetchByAssoc ( $db->query ( "SELECT i.id, i.document_no FROM ecminvoiceoutitems AS ii
INNER JOIN ecminvoiceouts AS i
ON ii.ecminvoiceout_id = i.id
AND ii.id = '" . $i ['old_ecminvoiceoutitem_id'] . "'" ) );
$items [$k] ['cor_document_no'] = $cor ['document_no'];
$items [$k] ['cor_id'] = $cor ['id'];
} else {
$items [$k] ['cor_document_no'] = '';
$items [$k] ['cor_id'] = '';
}
unset ( $inv );
unset ( $cor );
}
$items['items'] = $items;
$items['errors'] = $errors;
return $items;
}
function saveCorrect($items) {
$i = new EcmInvoiceOut ();
$a = new Account ();
$a->retrieve ( $this->parent_id );
$i->parent_id = $this->parent_id;
$i->parent_name = $this->parent_name;
$i->parent_address_street = $this->parent_address_street;
$i->parent_address_city = $this->parent_address_city;
$i->parent_address_postalcode = $this->parent_address_postalcode;
$i->parent_address_country = $this->parent_address_country;
$i->to_nip = $a->to_vatid;
$i->ecmpaymentcondition_id = $this->ecmpaymentcondition_id;
$i->supplier_code = $a->supplier_code;
$pc = new EcmPaymentCondition ();
$pc->retrieve ( $this->ecmpaymentcondition_id );
$i->ecmpaymentcondition_name = $pc->name;
$i->payment_date = $GLOBALS ['timedate']->to_display_date ( date ( "Y-m-d", mktime () + 3600 * 24 * $pc->days ) );
/*
* $s = new EcmSale (); $s->retrieve ( $r ['so_id'] ); // delivery address $i->parent_shipping_address_name = $s->parent_shipping_address_name; $i->parent_shipping_address_street = $s->parent_shipping_address_street; $i->parent_shipping_address_city = $s->parent_shipping_address_city; $i->parent_shipping_address_postalcode = $s->parent_shipping_address_postalcode; $i->parent_shipping_address_country = $s->parent_shipping_address_country;
*/
$i->template_id = '97700b0d-fbe9-e366-4016-4b260f058a47';
$i->template_name = "e5 Polska Sp. z o. o.";
$i->parent_contact_name = $this->parent_contact_name;
$i->parent_contact_title = $this->parent_contact_title;
$i->order_no = $this->name;
$i->description = $this->description;
$i->contact_id = $this->contact_id;
$i->name = $r ['name'];
$i->register_date = $GLOBALS ['timedate']->to_display_date ( date ( "Y-m-d" ) );
$i->sell_date = $GLOBALS ['timedate']->to_display_date ( date ( "Y-m-d" ) );
$i->status = "accepted";
$i->type = "correct";
global $current_user;
$i->assigned_user_id = $current_user->id;
$i->created_by = $current_user->id;
$i->modified_user_id = $current_user->id;
$i->number = $i->generateNumberCorrect ();
$i->document_no = 'FVKOR ' . $i->formatNumberCorrect ();
$i->pdf_type = 'K';
// $i->currency_id=$s->currency_id;
// set possition list
$i->position_list = $items;
$no = $i->save ();
$db = $GLOBALS ['db'];
if ($no != '') {
$db->query ( "update ecmreturns set correct_id='" . $no . "' where id='" . $this->id . "'" );
}
return $no;
}
// mz generate number
function generateNumber() {
$db = $GLOBALS ['db'];
$r = $db->fetchByAssoc ( $db->query ( "SELECT count(id) as c FROM ecmreturns WHERE deleted='0' AND YEAR(register_date)='" . date ( 'Y' ) . "'" ) );
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" ) . $tmp;
}
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 'ZW ' . $n . '/' . $y;
}
}
?>