From 5446be55c047da9ec4fe9b48a8cc7ecca2f75ded Mon Sep 17 00:00:00 2001 From: danish Date: Mon, 8 Nov 2021 13:48:16 +0300 Subject: [PATCH] - Sales Api Cancel invoice method update ( working now ) - Purchase api for posting and canceling invoice added - Customer Payment posting and canceling invoice api added - In customer creation api response ( added branch id which automatically gets created for a new customer ) --- index.php | 41 +++++++ purchase.inc | 204 +++++++++++++++++++++++++++++++++ sales.inc | 84 +++++++++++++- src/CustomerPayments.php | 21 ++++ src/Customers.php | 1 + src/Purchase.php | 21 ++++ tests/CustomerPayment_Test.php | 37 ++++++ tests/Purchase_Test.php | 63 ++++++++++ tests/Sales_Test.php | 8 ++ 9 files changed, 479 insertions(+), 1 deletion(-) create mode 100755 purchase.inc create mode 100644 src/CustomerPayments.php create mode 100644 src/Purchase.php create mode 100644 tests/CustomerPayment_Test.php create mode 100644 tests/Purchase_Test.php diff --git a/index.php b/index.php index cf8653a..2bb7b9f 100755 --- a/index.php +++ b/index.php @@ -461,6 +461,12 @@ function assets_supported() $rest->delete('/:branch_id/:uuid', function ($branch_id, $uuid) use ($rest) { $rest->sales->delete($rest, $branch_id, $uuid); }); + + //added by Danish + $rest->post('/delete/:branch_id/:uuid', function ($branch_id, $uuid) use ($rest) { + $rest->sales->delete($rest, $branch_id, $uuid); + }); + // All Sales $rest->get('/:trans_type/', function ($trans_type) use ($rest) { $rest->sales->get($rest, $trans_type); @@ -524,5 +530,40 @@ function assets_supported() }); // ------------------------------ Journal ------------------------------- + + +// ------------------------------- Purchase ( Added by danish start ) -------------------------------- +$rest->container->singleton('purchase', function () { + return new \FAAPI\Purchase(); +}); +$rest->group('/purchase', function () use ($rest) { + $rest->post('/', function () use ($rest) { + $rest->purchase->post($rest); + }); + $rest->delete('/:branch_id/:uuid', function ($branch_id, $uuid) use ($rest) { + $rest->purchase->delete($rest, $branch_id, $uuid); + }); + //added by Danish + $rest->post('/delete/:branch_id/:uuid', function ($branch_id, $uuid) use ($rest) { + $rest->purchase->delete($rest, $branch_id, $uuid); + }); +}); +// ------------------------------- Purchase ( Added by danish end ) -------------------------------- + + +// ------------------------------- Customer Payments ( Added by danish start ) -------------------------------- +$rest->container->singleton('customerPayments', function () { + return new \FAAPI\customerPayments(); +}); +$rest->group('/customerPayments', function () use ($rest) { + $rest->post('/', function () use ($rest) { + $rest->customerPayments->post($rest); + }); + $rest->post('/delete', function () use ($rest) { + $rest->customerPayments->delete($rest); + }); +}); +// ------------------------------- Customer Payments ( Added by danish end ) -------------------------------- + // Init API $rest->run(); diff --git a/purchase.inc b/purchase.inc new file mode 100755 index 0000000..009f897 --- /dev/null +++ b/purchase.inc @@ -0,0 +1,204 @@ + + */ + +$path_to_root = "../.."; + +$page_security = 'SA_PURCHASEORDER'; +include_once($path_to_root . "/sales/includes/cart_class.inc"); +include_once($path_to_root . "/purchasing/includes/po_class.inc"); +//include_once($path_to_root . "/includes/session.inc"); +include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc"); +include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc"); +include_once($path_to_root . "/reporting/includes/reporting.inc"); +include_once($path_to_root . "/includes/types.inc"); + + +function purchase_add() { + + $app = \Slim\Slim::getInstance('SASYS'); + $req = $app->request(); + $info = $req->post(); + /*$cart = null; + $cart = new Cart($info['trans_type'], 20);*/ + + $purchase_obj=new purch_order(); + foreach ($info as $key => $value) { + $purchase_obj->{$key} = $value; + } + $line_items=[]; + foreach ($purchase_obj->line_items as $k =>$v){ + $line_items[]=new po_line_details($v['line_no'], $v['stock_id'], $v['item_description'], $v['quantity'], $v['price'], $v['units'], $v['req_del_date'], $v['qty_inv'], $v['qty_received'], $v['grn_item_id']); + } + $purchase_obj->line_items=$line_items; + $purchase_obj->orig_order_date=$info['orig_order_date']; + $purchase_obj->tax_included=$info['tax_included']; + $purchase_obj->trans_type=ST_SUPPINVOICE; + $purchase_obj->reference=$info['ref']; + + $_SESSION['PO']=$purchase_obj; + if (can_commit()) { + $cart=$_SESSION['PO']; + $trans_no = add_direct_supp_trans($cart); + if($trans_no){ + api_success_response(sprintf(_("Invoice # %d has been entered."), $trans_no)); + }else{ + api_error(412, 'Failed to add invoice.'); + } + }else{ + api_error(500, 'Invoice data is invalid.'); + } + + +} + +function can_commit() +{ + if (!get_post('supplier_id')) + { + display_error(_("There is no supplier selected.")); + set_focus('supplier_id'); + return false; + } + + if (!is_date($_POST['OrderDate'])) + { + display_error(_("The entered order date is invalid.")); + set_focus('OrderDate'); + return false; + } + if (($_SESSION['PO']->trans_type == ST_SUPPRECEIVE || $_SESSION['PO']->trans_type == ST_SUPPINVOICE) + && !is_date_in_fiscalyear($_POST['OrderDate'])) { + display_error(_("The entered date is out of fiscal year or is closed for further data entry.")); + set_focus('OrderDate'); + return false; + } + + if (($_SESSION['PO']->trans_type==ST_SUPPINVOICE) && !is_date($_POST['due_date'])) + { + display_error(_("The entered due date is invalid.")); + set_focus('due_date'); + return false; + } + + if (!$_SESSION['PO']->order_no) + { + if (!check_reference(get_post('ref'), $_SESSION['PO']->trans_type)) + { + set_focus('ref'); + return false; + } + } + + if ($_SESSION['PO']->trans_type == ST_SUPPINVOICE && trim(get_post('supp_ref')) == false) + { + display_error(_("You must enter a supplier's invoice reference.")); + set_focus('supp_ref'); + return false; + } + if ($_SESSION['PO']->trans_type==ST_SUPPINVOICE + && is_reference_already_there($_SESSION['PO']->supplier_id, get_post('supp_ref'), $_SESSION['PO']->order_no)) + { + display_error(_("This invoice number has already been entered. It cannot be entered again.") . " (" . get_post('supp_ref') . ")"); + set_focus('supp_ref'); + return false; + } + if ($_SESSION['PO']->trans_type == ST_PURCHORDER && get_post('delivery_address') == '') + { + display_error(_("There is no delivery address specified.")); + set_focus('delivery_address'); + return false; + } + if (get_post('StkLocation') == '') + { + display_error(_("There is no location specified to move any items into.")); + set_focus('StkLocation'); + return false; + } + if (!db_has_currency_rates($_SESSION['PO']->curr_code, $_POST['OrderDate'], true)) + return false; + if ($_SESSION['PO']->order_has_items() == false) + { + display_error (_("The order cannot be placed because there are no lines entered on this order.")); + return false; + } + if (floatcmp(input_num('prep_amount'), $_SESSION['PO']->get_trans_total()) > 0) + { + display_error(_("Required prepayment is greater than total invoice value.")); + set_focus('prep_amount'); + return false; + } + + return true; +} + +function purchase_delete($branch_id,$uuid){ + $resp=['status'=>true,'msg'=>'']; + try { + $_POST['filterType']=ST_SUPPINVOICE; + if (check_valid_entries()==true) + { + $void_entry = get_voided_entry($_POST['filterType'], $_POST['trans_no']); + if ($void_entry != null) + { + $resp['status']=false; + $resp['error_code']=500; + $resp['msg']="The selected transaction has already been voided."; + + } else { + $msg = void_transaction($_POST['filterType'], $_POST['trans_no'], $_POST['date_'], $_POST['memo_']); + if (!$msg) { + $resp['status'] = true; + } else { + $resp['status'] = false; + $resp['error_code'] = 500; + $resp['msg'] = $msg; + } + } + } + } catch (Exception $e) { + error_log($e->getMessage(), 3, "/var/tmp/sales_cancel.log"); + $resp['msg']='Could not cancel invoice. '; + return; + } + if($resp['status']){ + api_success_response($resp); + }else{ + api_error($resp['error_code'], $resp['msg']); + } + +} + +function check_valid_entries() +{ + if (is_closed_trans($_POST['filterType'],$_POST['trans_no'])) + { + display_error(_("The selected transaction was closed for edition and cannot be voided.")); + set_focus('trans_no'); + return false; + } + if (!is_date($_POST['date_'])) + { + display_error(_("The entered date is invalid.")); + set_focus('date_'); + return false; + } + if (!is_date_in_fiscalyear($_POST['date_'])) + { + display_error(_("The entered date is out of fiscal year or is closed for further data entry.")); + set_focus('date_'); + return false; + } + + if (!is_numeric($_POST['trans_no']) OR $_POST['trans_no'] <= 0) + { + display_error(_("The transaction number is expected to be numeric and greater than zero.")); + set_focus('trans_no'); + return false; + } + return true; +} + + +?> \ No newline at end of file diff --git a/sales.inc b/sales.inc index 8b6cfcf..794ca2f 100755 --- a/sales.inc +++ b/sales.inc @@ -15,6 +15,7 @@ include_once($path_to_root . "/sales/includes/db/cust_trans_details_db.inc"); include_once($path_to_root . "/sales/includes/sales_db.inc"); include_once($path_to_root . "/sales/includes/db/sales_types_db.inc"); include_once($path_to_root . "/admin/db/attachments_db.inc"); +include_once($path_to_root . "/includes/types.inc"); // include_once($path_to_root . "/modules/timbrado/includes/db/comprobantes_db.php.inc"); // include_once($path_to_root . "/modules/timbrado/includes/db/llavero_db.php.inc"); // include_once($path_to_root . "/modules/timbrado/includes/generate/AutoFacturaCore.php.inc"); @@ -468,7 +469,7 @@ function sales_edit($trans_no, $trans_type) { api_success_response(_("Ok")); } -function sales_cancel($branch_id, $uuid) { +function sales_cancel__($branch_id, $uuid) { error_log("\r\n1", 3, "/var/tmp/sasys.log"); $AutoFactura = new AutoFacturaCore(); $AutoFactura->userkey = get_cofig("Llave de Usuario", ""); @@ -497,6 +498,87 @@ function sales_cancel($branch_id, $uuid) { } } + + +/** + * Author : Danish Naseem + * + */ + +function sales_cancel($branch_id, $uuid) { + $resp=['status'=>true,'msg'=>'']; + try { + $_POST['filterType']=ST_SALESINVOICE; + $check=check_valid_entries(); + + if ($check['status']) + { + $void_entry = get_voided_entry($_POST['filterType'], $_POST['trans_no']); + if ($void_entry != null) + { + $resp['status']=false; + $resp['error_code']=500; + $resp['msg']="The selected transaction has already been voided."; + } else { + $msg = void_transaction($_POST['filterType'], $_POST['trans_no'], $_POST['date_'], $_POST['memo_']); + if (!$msg) { + $resp['status'] = true; + } else { + $resp['status'] = false; + $resp['error_code'] = 500; + $resp['msg'] = $msg; + } + } + }else{ + $resp['status'] = false; + $resp['error_code'] = 500; + $resp['msg'] = $check['msg']; + } + } catch (Exception $e) { + error_log($e->getMessage(), 3, "/var/tmp/sales_cancel.log"); + $resp['msg']='Could not cancel invoice. '; + return; + } + if($resp['status']){ + api_success_response($resp); + }else{ + api_error($resp['error_code'], $resp['msg']); + } +} + +/** + * Author : Danish Naseem + * + */ +function check_valid_entries() +{ + $check=array('status'=>true,'msg'); + + if (is_closed_trans($_POST['filterType'],$_POST['trans_no'])) + { + $check['msg'] = "The selected transaction was closed for edition and cannot be voided."; + $check['status'] = false; + } + if (!is_date($_POST['date_'])) + { + $check['msg'] = "The entered date is invalid."; + $check['status'] = false; + } + if (!is_date_in_fiscalyear($_POST['date_'])) + { + $check['msg'] ="The entered date is out of fiscal year or is closed for further data entry."; + $check['status'] = false; + } + + if (!is_numeric($_POST['trans_no']) OR $_POST['trans_no'] <= 0) + { + $check['msg'] ="The transaction number is expected to be numeric and greater than zero."; + $check['status'] = false; + } + + return $check; +} + function can_process($info, $cart) { global $Refs; diff --git a/src/CustomerPayments.php b/src/CustomerPayments.php new file mode 100644 index 0000000..676ce2e --- /dev/null +++ b/src/CustomerPayments.php @@ -0,0 +1,21 @@ + + */ + +namespace FAAPI; + +class CustomerPayments +{ + public function post($rest) + { + include_once(API_ROOT . "/customerPayments.inc"); + customerPayments_add(); + } + + public function delete($rest) + { + include_once(API_ROOT . "/customerPayments.inc"); + customerPayments_delete(); + } +} diff --git a/src/Customers.php b/src/Customers.php index 4a80e24..99f1f0f 100644 --- a/src/Customers.php +++ b/src/Customers.php @@ -129,6 +129,7 @@ public function post($rest) } $cust = get_customer($selected_id); if ($cust != null) { + $cust['selected_branch']=$selected_branch; api_create_response(json_encode($cust)); } else { api_error(500, 'Could Not Save to Database'); diff --git a/src/Purchase.php b/src/Purchase.php new file mode 100644 index 0000000..9f99321 --- /dev/null +++ b/src/Purchase.php @@ -0,0 +1,21 @@ + + */ + +namespace FAAPI; + +class Purchase +{ + public function post($rest) + { + include_once(API_ROOT . "/purchase.inc"); + purchase_add(); + } + + public function delete($rest, $branch_id, $uuid) + { + include_once(API_ROOT . "/purchase.inc"); + purchase_delete($branch_id, $uuid); + } +} diff --git a/tests/CustomerPayment_Test.php b/tests/CustomerPayment_Test.php new file mode 100644 index 0000000..c8063f9 --- /dev/null +++ b/tests/CustomerPayment_Test.php @@ -0,0 +1,37 @@ + + */ + + +// Posting new Customer Payment Invoice - POST Request +$info=array ( + 'customer_id' => 2, + 'BranchID' => 3, + 'bank_account' => '1', + 'trans_no' => '', + 'DateBanked' => '10/10/2021', + 'ref' => '001', + 'charge' => '', + 'dimension_id' => '0', + 'dimension2_id' => '0', + 'TotalNumberOfAllocs' => '0', + 'discount' => '', + 'amount' => 10, + 'memo_' => substr('Comment should be limited to 255 characters only. As this is setup in the database field size.',0,255), + 'AddPaymentItem' => 'Add Payment', +); + + +// Deleting / Voiding Customer Payment Invoice - POST Request +$info=array ( + 'trans_no' => 3, + 'date_' => '10/10/2021', + 'memo_' => substr('Comment should be limited to 255 characters only. As this is setup in the database field size',0,255), +); + +class CustomerPayment_Test extends Crud_Base +{ + +} + diff --git a/tests/Purchase_Test.php b/tests/Purchase_Test.php new file mode 100644 index 0000000..85a965c --- /dev/null +++ b/tests/Purchase_Test.php @@ -0,0 +1,63 @@ + + */ + + +// Posting new Purchase Invoice - POST Request +$info=array ( + 'supplier_id' => 2, + 'OrderDate' => '10/10/2021', + 'ref' => '001', + 'due_date' => '10/10/2021', + 'supp_ref' => '001', + 'dimension' => 0, + 'dimension2' => 0, + 'StkLocation' => 'DEF', + 'delivery_address' => 'N/A', + '_stock_id_edit' => '', + 'qty' => '1', + 'cash_account' => '', + 'Comments' => substr('Comment should be limited to 255 characters only. As this is setup in the database field size',0,255), + 'Commit' => 'Process Invoice', + 'sales_type' => '1', + 'curr_code' => 'USD', + 'orig_order_date' => '10/10/2021', + 'tax_included' => 0, + 'Location' => 'DEF', + 'line_items' =>[ + [ + 'line_no' => 0, + 'po_detail_rec' => NULL, + 'grn_item_id' => 0, + 'stock_id' => 'stock_001', + 'item_description' => '', + 'price' => 10.5, + 'units' => 'each', + 'req_del_date' => '', + 'tax_type' => '0', + 'tax_type_name' => 'Regular', + 'quantity' => 1, + 'qty_inv' => 0, + 'receive_qty' => 0, + 'qty_received' => 0, + 'standard_cost' => 0, + 'descr_editable' => '0', + ] + ] + +); + + +// Deleting / Voiding Purchase Invoice - POST Request +$info=array ( + 'trans_no' => 3, + 'date_' => '10/10/2021', + 'memo_' => substr('Comment should be limited to 255 characters only. As this is setup in the database field size',0,255), +); + +class Purchase_Test extends Crud_Base +{ + +} + diff --git a/tests/Sales_Test.php b/tests/Sales_Test.php index 338b12c..a9694d0 100644 --- a/tests/Sales_Test.php +++ b/tests/Sales_Test.php @@ -232,5 +232,13 @@ public function testCRUD_Ok() $count2 = count($result); $this->assertEquals($count0, $count2); */ + + // Deleting / Voiding Purchase Invoice - POST Request + $info=array ( + 'trans_no' => 3, + 'date_' => '10/10/2021', + 'memo_' => substr('Comment should be limited to 255 characters only. As this is setup in the database field size',0,255), + ); + } }