fadlyshafa

Untitled

Jun 23rd, 2020
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. require_once APPPATH . '/../vendor/autoload.php';
  6.  
  7. class Sales extends CI_Controller {
  8.  
  9. function __construct() {
  10. parent::__construct();
  11. $this->load->model('sales_model');
  12. $this->load->model('quotation_model');
  13. $this->load->model('customer_model');
  14. $this->load->model('category_model');
  15. $this->load->model('brand_model');
  16. $this->load->model('bank_model');
  17. $this->load->model('purchase_model');
  18. $this->load->model('company_setting_model');
  19. $this->load->model('kurs_model');
  20. $this->load->model('signature_model');
  21. $this->load->model('general_model');
  22. $this->load->model('log_model');
  23. }
  24.  
  25. public function randomKey($length) {
  26. $key = '';
  27. $pool = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
  28. for ($i = 0; $i < $length; $i++) {
  29. $key .= $pool[mt_rand(0, count($pool) - 1)];
  30. }
  31. return $key;
  32. }
  33.  
  34. public function alert($msg) {
  35. echo "<script type='text/javascript'>system.log('$msg');</script>";
  36. }
  37.  
  38. public function index() {
  39. // get all sales to display list
  40. $filter = null;
  41. if(isset($_GET['filter'])) {
  42. if ($_GET['filter'] != null) {
  43. $filter = $_GET['filter'];
  44. }
  45. }
  46.  
  47. $data['quotation'] = $this->quotation_model->getQuotation();
  48. $data['data'] = $this->sales_model->getSales($filter);
  49. $this->load->view('sales/list', $data);
  50. }
  51.  
  52. /*
  53. call add view to add sales record
  54. */
  55.  
  56. public function add() {
  57. $kursdata = $this->company_setting_model->getCurrency();
  58.  
  59. $data['country'] = $this->db->get('countries')->result();
  60. $data['brands'] = $this->brand_model->getBrand();
  61. $data['warehouse'] = $this->sales_model->getWarehouse();
  62. $data['quotation'] = $this->quotation_model->getQuotation();
  63. $data['warehouse_products'] = $this->sales_model->getWarehouseProducts();
  64. $data['tax'] = $this->sales_model->getTax();
  65. $data['biller'] = $this->sales_model->getBiller();
  66. $data['customer'] = $this->sales_model->getCustomer();
  67. $data['quatation'] = $this->sales_model->getQuatation();
  68. $data['product'] = $this->sales_model->getProducts2();
  69. $data['service'] = $this->sales_model->getService();
  70. $data['discount'] = $this->sales_model->getDiscount();
  71. $data['kursdata'] = $kursdata;
  72. $data['reference_no'] = $this->sales_model->createReferenceNo();
  73. $data['so_number'] = $this->general_model->generateAutoNumber('sales_order');
  74. $this->load->view('sales/add', $data);
  75. }
  76.  
  77. /*
  78. this function is used to get discount data when discount is change
  79. */
  80.  
  81. public function getDiscountAjax($id) {
  82. $data = $this->sales_model->getDiscountAjax($id);
  83. echo json_encode($data);
  84. }
  85.  
  86. /* get all product warehouse wise */
  87.  
  88. public function getProducts($warehouse_id) {
  89. $data = $this->sales_model->getProducts($warehouse_id);
  90. echo json_encode($data);
  91. }
  92.  
  93. public function getPartNumber($brand_id) {
  94. $data = $this->sales_model->getPartNumber($brand_id);
  95. echo json_encode($data);
  96. }
  97.  
  98. public function getCodeProduct($code) {
  99. $data = $this->sales_model->getCodeProduct($code);
  100. echo json_encode($data);
  101. }
  102.  
  103. /* get single product */
  104.  
  105. public function getProduct($product_id,$sales_id) {
  106. $datas = $this->sales_model->getProducts21($product_id);
  107. // print_r(var_dump($this->db->last_query()));
  108. for ($i=0; $i < count($datas) ; $i++) {
  109.  
  110. if ($datas[$i]->product_grouping != NULL) {
  111. $arlist = json_encode(unserialize($datas[$i]->list));
  112. $arlist = json_decode($arlist);
  113. $max = array();
  114. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  115.  
  116. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  117. $count = ($qtyp / intval($arlist[$ilist]->ql));
  118. array_push($max, $count);
  119. }
  120. $min = min($max);
  121. $datas[$i]->quantity = intval($min);
  122. }
  123. }
  124.  
  125. $data['items'] = $this->sales_model->getProducts99($sales_id);
  126.  
  127. $data['discount'] = $this->sales_model->getDiscount();
  128. $data['bonus'] = $this->sales_model->getBonus($product_id);
  129. $data['bonus1'] = $this->sales_model->getBonus12();
  130. $data['tax'] = $this->sales_model->getTax();
  131. echo json_encode($data);
  132. }
  133.  
  134. public function getProduct_old($product_id) {
  135. $data = $this->sales_model->getProducts21($product_id);
  136. // print_r(var_dump($this->db->last_query()));
  137. for ($i=0; $i < count($data) ; $i++) {
  138.  
  139. if ($data[$i]->product_grouping != NULL) {
  140. $arlist = json_encode(unserialize($data[$i]->list));
  141. $arlist = json_decode($arlist);
  142. $max = array();
  143. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  144.  
  145. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  146. $count = ($qtyp / intval($arlist[$ilist]->ql));
  147. array_push($max, $count);
  148. }
  149. $min = min($max);
  150. $data[$i]->quantity = intval($min);
  151. }
  152. }
  153.  
  154. // $data['items'] = $this->sales_model->getProducts99($sales_id);
  155.  
  156. $data['discount'] = $this->sales_model->getDiscount();
  157. $data['bonus'] = $this->sales_model->getBonus($product_id);
  158. $data['bonus1'] = $this->sales_model->getBonus12();
  159. $data['tax'] = $this->sales_model->getTax();
  160. echo json_encode($data);
  161. }
  162.  
  163. public function getService($service_id) {
  164. $data = $this->sales_model->getServicetable($service_id);
  165. $data['discount'] = $this->sales_model->getDiscount();
  166. $data['tax'] = $this->sales_model->getTax();
  167. echo json_encode($data);
  168. }
  169.  
  170. /*
  171. this function is used to search product name / code in auto complite
  172. */
  173.  
  174. public function getAutoCodeName($code, $search_option, $warehouse) {
  175. //$code = strtolower($code);
  176. $p_code = $this->input->post('p_code');
  177. $p_search_option = $this->input->post('p_search_option');
  178. $data = $this->sales_model->getProductCodeName($p_code, $p_search_option, $warehouse);
  179. if ($search_option == "Code") {
  180. $list = "<ul class='auto-product'>";
  181. foreach ($data as $val) {
  182. $list .= "<li value=" . $val->code . ">" . $val->code . "</li>";
  183. }
  184. $list .= "</ul>";
  185. } else {
  186. $list = "<ul class='auto-product'>";
  187. foreach ($data as $val) {
  188. $list .= "<li value=" . $val->product_id . ">" . $val->name . "</li>";
  189. }
  190. $list .= "</ul>";
  191. }
  192.  
  193. echo $list;
  194. //echo json_encode($data);
  195. //print_r($data);
  196. }
  197.  
  198. /*
  199. this fucntion is used to add sales record in database
  200. */
  201.  
  202. public function addSales() {
  203. date_default_timezone_set("Asia/Bangkok");
  204.  
  205. $submit = $this->input->post('submit');
  206. if ($submit == "Publish") {
  207. $status = 0;
  208. } else {
  209. $status = 4;
  210. }
  211.  
  212. $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  213. if ($this->form_validation->run() == false) {
  214. $this->add();
  215. } else {
  216. $data = array(
  217. "po" => $this->input->post('so'),
  218. "po_cust" => $this->input->post('po_cust') ?: "",
  219. "cc_holder" => $this->input->post('taxx'),
  220. "currentdate" => $this->input->post('sales_date'),
  221. "date" => $this->input->post('cust_date'),
  222. "reference_no" => serialize($this->input->post('reference_no')),
  223. "ketnama" => $this->input->post('reference'),
  224. "warehouse_id" => "",
  225. "biller_id" => $this->input->post('biller_id'),
  226. "customer_id" => $this->input->post('customer'),
  227. "upcc" => $this->input->post('main'),
  228. "upcc2" => $this->input->post('cc'),
  229. "total" => $this->input->post('grand_total'),
  230. "discount_value" => $this->input->post('total_discount'),
  231. "tax_value" => $this->input->post('total_tax'),
  232. "note" => $this->input->post('note'),
  233. "shipping_city_id" => $this->input->post('city'),
  234. "shipping_state_id" => $this->input->post('state'),
  235. "shipping_country_id" => $this->input->post('country'),
  236. "shipping_address" => $this->input->post('address'),
  237. "shipping_charge" => $this->input->post('shipping_charge'),
  238. "internal_note" => $this->input->post('internal_note'),
  239. "mode_of_transport" => $this->input->post('mode_of_transport'),
  240. "transporter_name" => $this->input->post('transporter_name'),
  241. "transporter_code" => $this->input->post('transporter_code'),
  242. "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  243. "l_r_no" => $this->input->post('l_r_no'),
  244. "chalan_no" => $this->input->post('chalan_no'),
  245. "indent_no" => $this->input->post('indent_no'),
  246. "credit_days" => $this->input->post('credit_days'),
  247. "date_of_supply" => $this->input->post('date_of_supply'),
  248. "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  249. "gst_payable" => $this->input->post('gst_payable'),
  250. "broker" => $this->input->post('broker'),
  251. "user" => $this->session->userdata('user_id'),
  252. "term_and_condition" => $this->input->post('term_and_condition'),
  253. "user_name" => $this->session->userdata('identity'),
  254. "kurs_id" => $this->input->post('kursdata'),
  255. "nominalcurrency" => $this->input->post('grand_currency'),
  256. "ATTN" => $this->input->post('tax'),
  257. "shipto" => $this->input->post('shipto'),
  258. "billto" => $this->input->post('billto'),
  259. "dateestimate" => $this->input->post('dateestimate'),
  260. "salesdesc" => $this->input->post('others'),
  261. "paid_by" => $this->input->post('paymenterms') ?: 'cash',
  262. "diskonpersen" => $this->input->post('DiscountPersen'),
  263. "service_description" => $this->input->post('service_description'),
  264. "additional_description" => $this->input->post('additional_description'),
  265. "status_rev" => $status,
  266. "diskon" => $this->input->post('diskonfix')
  267. );
  268.  
  269. $invoice = array(
  270. "invoice_no" => $this->sales_model->generateInvoiceNo(),
  271. "sales_amount" => $this->input->post('grand_total'),
  272. "invoice_date" => date('Y-m-d')
  273. );
  274.  
  275. if ($sales_id = $this->sales_model->addModel($data, $invoice)) {
  276. $log_data = array(
  277. 'user_id' => $this->session->userdata('user_id'),
  278. 'table_id' => $sales_id,
  279. 'message' => 'Sales Inserted'
  280. );
  281. $this->log_model->insert_log($log_data);
  282. $jumlahtermin = $this->input->post('terminnumber');
  283.  
  284.  
  285. if (!empty($jumlahtermin)) {
  286. $termins = $this->input->post('terminvalue');
  287. $ket = $this->input->post('terminket');
  288. $tanggaltermins = $this->input->post('tanggalterminvalue');
  289. $i = 1;
  290. if (!empty($termins)) {
  291. $im = 0;
  292. $termino = 0;
  293. foreach ($termins as $quan) {
  294. $value = $this->input->post('grand_total');
  295. $nominal = ($value * $quan) / 100;
  296. $nominal = number_format((float) $nominal, 2, '.', '');
  297. if (!empty($tanggaltermins[$im])) {
  298. $datetermin = $tanggaltermins[$im];
  299. } else {
  300. $datetermin = date('Y-m-d');
  301. }
  302. $termino++;
  303. $datatermin = array(
  304. "sales_id" => $sales_id,
  305. "persen" => $quan,
  306. "nominal" => $nominal,
  307. "tanggalbayar" => $datetermin,
  308. "keterangan" => $ket[$i]
  309. );
  310.  
  311. $this->sales_model->addTerminModel($datatermin);
  312. $i++;
  313. $im++;
  314. }
  315. } else {
  316.  
  317. }
  318. }
  319.  
  320.  
  321. $js_data = json_decode($this->input->post('table_data'));
  322. $js_data1 = json_decode($this->input->post('tableadditional_data'));
  323. $js_data2 = json_decode($this->input->post('tableservice_data'));
  324. $js_data3 = json_decode($this->input->post('tableitemrequest'));
  325.  
  326. // if ($js_data) {
  327. // foreach ($js_data as $key => $value) {
  328. // if ($value != null || !empty($value->product_id)) {
  329.  
  330. // $data = array(
  331. // "product_id" => $value->product_id,
  332. // "quantity" => $value->quantity,
  333. // "price" => $value->price,
  334. // "gross_total" => $value->total,
  335. // "discount_id" => @$value->discount_id,
  336. // "discount_value" => @$value->discount_value,
  337. // "discount" => $value->discount,
  338. // "serial_no" => $value->desc,
  339. // "sales_id" => $sales_id
  340. // );
  341.  
  342. // $this->db->insert('sales_items' ,$data);
  343. // }
  344. // }
  345. // }
  346.  
  347. $product_id = $this->input->post('product_id');
  348.  
  349. foreach ($product_id as $e=>$pd) {
  350. $quantity = $this->input->post('qty'.$e);
  351. $price = $this->input->post('price'.$e);
  352. $gross_total = $this->input->post('subtotal'.$e);
  353. $discount_id = $this->input->post('discount_id'.$e);
  354. $discount_value = $this->input->post('discount_value'.$e);
  355. $discount = $this->input->post('discount'.$e);
  356. $serial_no = $this->input->post('serial_no'.$e);
  357. $sales_id = $sales_id;
  358.  
  359. $lines['quantity'] = $quantity;
  360. $lines['price'] = $price;
  361. $lines['gross_total'] = $gross_total;
  362. $lines['discount_id'] = $discount_id;
  363. $lines['discount_value'] = $discount_value;
  364. $lines['discount'] = $discount;
  365. $lines['serial_no'] = $serial_no;
  366. $lines['sales_id'] = $sales_id;
  367.  
  368. $this->db->insert('sales_items' ,$lines);
  369. }
  370.  
  371. if ($js_data1) {
  372.  
  373. foreach ($js_data1 as $key => $value) {
  374. if ($value != null || !empty($value->additional_id)) {
  375.  
  376. $data = array(
  377. "product_id" => $value->additional_id,
  378. "quantity" => $value->quantity,
  379. "price" => $value->price,
  380. "gross_total" => $value->total,
  381. "discount_id" => @$value->discount_id,
  382. "discount_value" => @$value->discount_value,
  383. "discount" => $value->discount,
  384. "desc" => $value->desc,
  385. "sales_id" => $sales_id
  386. );
  387.  
  388. $this->db->insert('sales_additional_items' ,$data);
  389. }
  390. }
  391. }
  392.  
  393. if ($js_data2) {
  394.  
  395. foreach ($js_data2 as $key => $value) {
  396. if ($value != null || !empty($value->service_id)) {
  397.  
  398. $data = array(
  399. "service_id" => $value->service_id,
  400. "quantity" => @$value->quantity?:0,
  401. "price" => @$value->price?:0,
  402. "gross_total" => $value->total,
  403. "discount_id" => @$value->discount_id,
  404. "discount_value" => @$value->discount_value,
  405. "discount" => $value->discount,
  406. "sales_id" => $sales_id
  407. );
  408.  
  409. $this->sales_model->addSalesServiceItem($data);
  410.  
  411. }
  412. }
  413. }
  414.  
  415. if ($js_data3) {
  416. var_dump($js_data3);
  417. foreach ($js_data3 as $value) {
  418. var_dump($value->request_date);
  419.  
  420. //random name item req
  421. $digits = 4;
  422. $rand = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
  423. $number = date("dm").$rand;
  424. $cek = $this->db->select('*')->from('item_request')->where('sales_id', $sales_id)->get()->result();
  425. var_dump($cek);
  426. if (empty($cek)) {
  427. $deadline = date('Y-m-d', strtotime('14 day', strtotime(date($value->request_date))));
  428.  
  429. $params = array(
  430. 'request_number' => $number,
  431. 'request_date' => $value->request_date,
  432. 'request_deadline' => $deadline,
  433. 'reference' => $value->reference,
  434. 'sales_id' => $sales_id,
  435. 'request_for' => 'Sales'
  436. );
  437.  
  438. $this->db->insert('item_request', $params);
  439. $id = $this->db->insert_id();
  440. } else {
  441. $id = $cek[0]->id_item_request;
  442. }
  443.  
  444. $params = array(
  445. 'item_request_id' => $id,
  446. 'product_id' => $value->id_product,
  447. 'price' => $value->price,
  448. 'qty' => $value->qty
  449. );
  450.  
  451.  
  452.  
  453. $this->db->insert('item_request_items', $params);
  454.  
  455. }
  456.  
  457. }
  458.  
  459.  
  460. redirect('sales/view/' . $sales_id);
  461. } else {
  462. redirect('sales', 'refresh');
  463. }
  464. }
  465. }
  466.  
  467. public function revisiSales() {
  468. $id = $this->input->post('sales_id');
  469. $status_old = $this->input->post('status_old');
  470. date_default_timezone_set("Asia/Bangkok");
  471.  
  472. $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  473.  
  474. $submit = $this->input->post('submit');
  475. if ($submit == "Publish") {
  476. $status = 1;
  477. } else {
  478. $status = 4;
  479. }
  480. if ($this->form_validation->run() == false) {
  481. $this->revisi($id);
  482. } else {
  483. $data = array(
  484. "po" => $this->input->post('po'),
  485. "cc_holder" => $this->input->post('taxx'),
  486. "po_cust" => $this->input->post('po_cust'),
  487. "po_new" => $this->input->post('po_new'),
  488. "currentdate" => $this->input->post('salesdate'),
  489. "date" => $this->input->post('cust_date'),
  490. "ketnama" => $this->input->post('reference'),
  491. "reference_no" => serialize($this->input->post('reference_no')),
  492. "warehouse_id" => "",
  493. "customer_id" => $this->input->post('customer'),
  494. "upcc" => $this->input->post('main'),
  495. "upcc2" => $this->input->post('cc'),
  496. "total" => $this->input->post('grand_total'),
  497. "discount_value" => $this->input->post('total_discount'),
  498. "tax_value" => $this->input->post('total_tax'),
  499. "note" => $this->input->post('note'),
  500. "shipping_city_id" => $this->input->post('city'),
  501. "shipping_state_id" => $this->input->post('state'),
  502. "shipping_country_id" => $this->input->post('country'),
  503. "shipping_address" => $this->input->post('address'),
  504. "shipping_charge" => $this->input->post('shipping_charge'),
  505. "internal_note" => $this->input->post('internal_note'),
  506. "mode_of_transport" => $this->input->post('mode_of_transport'),
  507. "transporter_name" => $this->input->post('transporter_name'),
  508. "transporter_code" => $this->input->post('transporter_code'),
  509. "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  510. "l_r_no" => $this->input->post('l_r_no'),
  511. "chalan_no" => $this->input->post('chalan_no'),
  512. "indent_no" => $this->input->post('indent_no'),
  513. "credit_days" => $this->input->post('credit_days'),
  514. "date_of_supply" => $this->input->post('date_of_supply'),
  515. "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  516. "gst_payable" => $this->input->post('gst_payable'),
  517. "broker" => $this->input->post('broker'),
  518. "user" => $this->session->userdata('user_id'),
  519. "term_and_condition" => $this->input->post('term_and_condition'),
  520. "user_name" => $this->session->userdata('identity'),
  521. "kurs_id" => $this->input->post('kursdata'),
  522. "nominalcurrency" => $this->input->post('grand_currency'),
  523. "ATTN" => $this->input->post('tax'),
  524. "shipto" => $this->input->post('shipto'),
  525. "billto" => $this->input->post('billto'),
  526. "dateestimate" => $this->input->post('dateestimate'),
  527. "salesdesc" => $this->input->post('others'),
  528. "paid_by" => $this->input->post('paymenterms') ?: 'advance',
  529. "additional_description" => $this->input->post('additional_description'),
  530. "service_description" => $this->input->post('service_description'),
  531. "diskonpersen" => $this->input->post('DiscountPersen'),
  532. "status_rev" => $status,
  533. "diskon" => $this->input->post('diskonfix')
  534. );
  535.  
  536. $invoice = array(
  537. "invoice_no" => $this->sales_model->generateInvoiceNo(),
  538. "sales_amount" => $this->input->post('grand_total'),
  539. "invoice_date" => date('Y-m-d')
  540. );
  541.  
  542. if ($sales_id = $this->sales_model->addModel($data, $invoice)) {
  543.  
  544. if ($status_old == 2) {
  545.  
  546. $product_items = $this->db->select('*')
  547. ->from('sales_items')
  548. ->where('sales_id', $id)
  549. ->get()
  550. ->result();
  551.  
  552. foreach ($product_items as $row) {
  553. $idp = $this->db->select('*')
  554. ->from('product_grouping')
  555. ->where('product_id', $row->product_id)
  556. ->get()
  557. ->result();
  558.  
  559. if ($idp) {
  560. $arlist = json_encode(unserialize($idp[0]->list));
  561. $arlist = json_decode($arlist);
  562.  
  563. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  564.  
  565. $quantity = $row->quantity * $arlist[$ilist]->ql;
  566.  
  567. $this->db->set('quantity', 'quantity'-$quantity)
  568. ->where('product_id', $arlist[$ilist]->pl)
  569. ->update('products');
  570. }
  571. } else {
  572. $this->db->query("UPDATE products SET quantity=quantity + $row->quantity WHERE product_id=$row->product_id");
  573. }
  574. }
  575.  
  576. $additional_items = $this->db->select('*')
  577. ->from('sales_additional_items')
  578. ->where('sales_id', $id)
  579. ->get()
  580. ->result();
  581.  
  582. foreach ($additional_items as $value) {
  583. $idp = $this->db->select('*')
  584. ->from('product_grouping')
  585. ->where('product_id', $value->product_id)
  586. ->get()
  587. ->result();
  588.  
  589. if ($idp) {
  590. $arlist = json_encode(unserialize($idp[0]->list));
  591. $arlist = json_decode($arlist);
  592.  
  593. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  594.  
  595. $quantity = $value->quantity * $arlist[$ilist]->ql;
  596.  
  597. $this->db->set('quantity', 'quantity'-$quantity)
  598. ->where('product_id', $arlist[$ilist]->pl)
  599. ->update('products');
  600. }
  601. } else {
  602. $this->db->query("UPDATE products SET quantity=quantity + $row->quantity WHERE product_id=$value->product_id");
  603. }
  604. }
  605. }
  606. $log_data = array(
  607. 'user_id' => $this->session->userdata('user_id'),
  608. 'table_id' => $sales_id,
  609. 'message' => 'Sales Inserted'
  610. );
  611. $this->log_model->insert_log($log_data);
  612. $jumlahtermin = $this->input->post('terminnumber');
  613.  
  614.  
  615. if (!empty($jumlahtermin)) {
  616. $termins = $this->input->post('terminvalue');
  617. $ket = $this->input->post('terminket');
  618. $tanggaltermins = $this->input->post('tanggalterminvalue');
  619. $i = 1;
  620. if (!empty($termins)) {
  621. $im = 0;
  622. $termino = 0;
  623. foreach ($termins as $quan) {
  624. $value = $this->input->post('grand_total');
  625. $nominal = ($value * $quan) / 100;
  626. $nominal = number_format((float) $nominal, 2, '.', '');
  627. if (!empty($tanggaltermins[$im])) {
  628. $datetermin = $tanggaltermins[$im];
  629. } else {
  630. $datetermin = date('Y-m-d');
  631. }
  632. $termino++;
  633. $datatermin = array(
  634. "sales_id" => $sales_id,
  635. "persen" => $quan,
  636. "nominal" => $nominal,
  637. "tanggalbayar" => $datetermin,
  638. "keterangan" => $ket[$i]
  639. );
  640.  
  641. $this->sales_model->addTerminModel($datatermin);
  642. $i++;
  643. $im++;
  644. }
  645. }
  646. }
  647.  
  648.  
  649. $js_data = json_decode($this->input->post('table_data'));
  650. $js_data1 = json_decode($this->input->post('tableadditional_data'));
  651. $js_data2 = json_decode($this->input->post('tableservice_data'));
  652.  
  653. foreach ($js_data as $key => $value) {
  654. if ($value != null || !empty($value->product_id)) {
  655.  
  656. $data = array(
  657. "product_id" => $value->product_id,
  658. "quantity" => $value->quantity,
  659. "price" => $value->price,
  660. "gross_total" => $value->total,
  661. "discount_id" => @$value->discount_id,
  662. "discount_value" => @$value->discount_value,
  663. "discount" => $value->discount,
  664. "serial_no" => $value->desc,
  665. "sales_id" => $sales_id
  666. );
  667.  
  668. $this->db->insert('sales_items', $data);
  669. }
  670. }
  671.  
  672. foreach ($js_data1 as $key => $value) {
  673. if ($value != null || !empty($value->additional_id)) {
  674.  
  675. $data = array(
  676. "product_id" => $value->additional_id,
  677. "quantity" => $value->quantity,
  678. "price" => $value->price,
  679. "gross_total" => $value->total,
  680. "discount_id" => @$value->discount_id,
  681. "discount_value" => @$value->discount_value,
  682. "discount" => $value->discount,
  683. "desc" => $value->desc,
  684. "sales_id" => $sales_id
  685. );
  686.  
  687. $this->db->insert('sales_additional_items' ,$data);
  688. }
  689. }
  690.  
  691. if (!empty($js_data2)) {
  692. foreach ($js_data2 as $key => $value) {
  693. if ($value != null || !empty($value->service_id)) {
  694.  
  695. $data = array(
  696. "service_id" => $value->service_id,
  697. "quantity" => $value->quantity,
  698. "price" => $value->price,
  699. "gross_total" => $value->total,
  700. "discount_id" => @$value->discount_id,
  701. "discount_value" => @$value->discount_value,
  702. "discount" => $value->discount,
  703. "sales_id" => $sales_id
  704. );
  705. $this->sales_model->addSalesServiceItem($data);
  706. }
  707. }
  708. }
  709.  
  710. $dataman = array(
  711. "status_rev" => 2
  712. );
  713.  
  714. if ($this->sales_model->editModel($id, $dataman)) {
  715.  
  716. $log_data = array(
  717. 'user_id' => $this->session->userdata('user_id'),
  718. 'table_id' => $id,
  719. 'message' => 'Change Revisi Sales Updated'
  720. );
  721. $this->log_model->insert_log($log_data);
  722. }
  723.  
  724.  
  725. redirect('sales/view/' . $sales_id);
  726. } else {
  727. redirect('sales', 'refresh');
  728. }
  729. }
  730. }
  731.  
  732. /*
  733. call edit view to edit sales record
  734. */
  735.  
  736. public function getCurrencyAjax($id) {
  737. $data = $this->sales_model->getCurrencyId($id);
  738. echo json_encode($data);
  739. //print_r($data);
  740. }
  741.  
  742. public function disetujui($id) {
  743. $data['data'] = $this->sales_model->getDetails($id);
  744. $data['quotation'] = $this->quotation_model->getQuotation();
  745. $data['check_invoice'] = $this->sales_model->checkInvoice($id);
  746. $data['items'] = $this->sales_model->getItems($id);
  747. $data['serviceitems'] = $this->sales_model->getServiceItems($id);
  748. $data['company'] = $this->purchase_model->getCompany();
  749. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  750. $data['quotationadditional'] = $this->quotation_model->getAdditional($id);
  751. $this->load->view('sales/disetujui', $data);
  752. }
  753.  
  754. public function changeSetujui() {
  755. $id = $this->input->post('sales_id');
  756.  
  757. $this->form_validation->set_rules('status', 'Status', 'required');
  758. if ($this->form_validation->run() == false) {
  759. $this->disetujui($id);
  760. } else {
  761.  
  762. $data = array(
  763. 'status_rev' => 2
  764. );
  765.  
  766. $array['id_medrep'] = $this->input->post("id_medrep");
  767. $array['id_sales'] = $this->input->post("id_sales");
  768. $array['total_sales'] = $this->input->post("total_sales");
  769. $this->sales_model->insert('medrep_transection',$array);
  770.  
  771. $product_items = $this->db->select('*')
  772. ->from('sales_items')
  773. ->where('sales_id', $id)
  774. ->get()
  775. ->result();
  776.  
  777. foreach ($product_items as $row) {
  778. $idp = $this->db->select('*')
  779. ->from('product_grouping')
  780. ->where('product_id', $row->product_id)
  781. ->get()
  782. ->result();
  783.  
  784. if ($idp) {
  785. $arlist = json_encode(unserialize($idp[0]->list));
  786. $arlist = json_decode($arlist);
  787.  
  788. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  789.  
  790. $quantity = $row->quantity * $arlist[$ilist]->ql;
  791.  
  792. $this->db->set('quantity', 'quantity'-$quantity)
  793. ->where('product_id', $arlist[$ilist]->pl)
  794. ->update('products');
  795. }
  796. } else {
  797. $this->db->query("UPDATE products SET quantity=quantity - $row->quantity WHERE product_id=$row->product_id");
  798. }
  799. }
  800.  
  801. $additional_items = $this->db->select('*')
  802. ->from('sales_additional_items')
  803. ->where('sales_id', $id)
  804. ->get()
  805. ->result();
  806.  
  807. foreach ($additional_items as $value) {
  808. $idp = $this->db->select('*')
  809. ->from('product_grouping')
  810. ->where('product_id', $value->product_id)
  811. ->get()
  812. ->result();
  813.  
  814. if ($idp) {
  815. $arlist = json_encode(unserialize($idp[0]->list));
  816. $arlist = json_decode($arlist);
  817.  
  818. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  819.  
  820. $quantity = $value->quantity * $arlist[$ilist]->ql;
  821.  
  822. $this->db->set('quantity', 'quantity'-$quantity)
  823. ->where('product_id', $arlist[$ilist]->pl)
  824. ->update('products');
  825. }
  826. } else {
  827. $this->db->query("UPDATE products SET quantity=quantity - $row->quantity WHERE product_id=$value->product_id");
  828. }
  829. }
  830.  
  831. $update = $this->db->where('sales_id', $id)->update('sales', $data);
  832.  
  833. if ($update) {
  834.  
  835. $log_data = array(
  836. 'user_id' => $this->session->userdata('user_id'),
  837. 'table_id' => $id,
  838. 'message' => 'Change Setujui Sales Updated'
  839. );
  840. $this->log_model->insert_log($log_data);
  841.  
  842. redirect('sales', 'refresh');
  843. } else {
  844.  
  845. redirect('sales', 'refresh');
  846. }
  847.  
  848.  
  849.  
  850. }
  851. }
  852.  
  853.  
  854. public function ditolak($id) {
  855. $data['data'] = $this->sales_model->getDetails($id);
  856. $data['quotation'] = $this->quotation_model->getQuotation();
  857. $data['check_invoice'] = $this->sales_model->checkInvoice($id);
  858. $data['items'] = $this->sales_model->getItems($id);
  859. $data['serviceitems'] = $this->sales_model->getServiceItems($id);
  860. $data['company'] = $this->purchase_model->getCompany();
  861. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  862. $data['quotationadditional'] = $this->quotation_model->getAdditional($id);
  863. $this->load->view('sales/ditolak', $data);
  864. }
  865.  
  866. public function changeTolak() {
  867. $id = $this->input->post('sales_id');
  868. $this->form_validation->set_rules('status', 'Status', 'required');
  869. $this->form_validation->set_rules('ditolak', 'Keterangan Ditolak', 'required');
  870. if ($this->form_validation->run() == false) {
  871. $this->ditolak($id);
  872. } else {
  873.  
  874. $data = array(
  875. 'status_rev' => 3
  876. );
  877.  
  878. $update = $this->db->where('sales_id', $id)->update('sales', $data);
  879.  
  880. if ($update) {
  881.  
  882. $log_data = array(
  883. 'user_id' => $this->session->userdata('user_id'),
  884. 'table_id' => $id,
  885. 'message' => 'Change Ditolak Sales Updated'
  886. );
  887.  
  888. $this->log_model->insert_log($log_data);
  889.  
  890. redirect('sales', 'refresh');
  891. } else {
  892.  
  893. redirect('sales', 'refresh');
  894. }
  895. }
  896. }
  897.  
  898. public function edit($id) {
  899.  
  900. $kursdata = $this->company_setting_model->getCurrency();
  901.  
  902. $data['brands'] = $this->brand_model->getBrand();
  903. $data['customer'] = $this->sales_model->getCustomer();
  904. $data['biller'] = $this->sales_model->getBiller();
  905. $data['product2'] = $this->sales_model->getProducts2();
  906. $data['service'] = $this->sales_model->getService();
  907. $data['additional'] = $this->sales_model->getAdditional($id);
  908. $data['discount'] = $this->sales_model->getDiscount();
  909. $data['bonus'] = $this->sales_model->getBonus($id);
  910. $data['tax'] = $this->sales_model->getTax();
  911. $data['data'] = $this->sales_model->getRecord($id);
  912. $data['country'] = $this->customer_model->getCountry();
  913. $data['kursdata'] = $kursdata;
  914. $data['state'] = $this->customer_model->getState($data['data'][0]->shipping_country_id);
  915. $data['city'] = $this->customer_model->getCity($data['data'][0]->shipping_state_id);
  916. $data['product'] = $this->sales_model->getProducts($data['data'][0]->warehouse_id);
  917. $data['items'] = $this->sales_model->getProducts99($id);
  918.  
  919. $data['items2_old'] = $this->sales_model->getProducts99($id);
  920. $data['total_items'] = count($data['items2_old']);
  921. // echo '<pre>' . var_export($data, true) . '</pre>';
  922. // die;
  923. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  924. $data['termin'] = $this->sales_model->getTerminSalesId($data['data'][0]->sales_id);
  925.  
  926. $this->load->view('sales/edit', $data);
  927. }
  928. public function revisi($id) {
  929.  
  930. $kursdata = $this->company_setting_model->getCurrency();
  931.  
  932. $data['brands'] = $this->brand_model->getBrand();
  933. $data['biller'] = $this->sales_model->getBiller();
  934. $data['customer'] = $this->sales_model->getCustomer();
  935. $data['product2'] = $this->sales_model->getProducts2();
  936. $data['service'] = $this->sales_model->getService();
  937. $data['additional'] = $this->sales_model->getAdditional($id);
  938. $data['discount'] = $this->sales_model->getDiscount();
  939. $data['tax'] = $this->sales_model->getTax();
  940. $data['data'] = $this->sales_model->getRecord($id);
  941. $data['country'] = $this->customer_model->getCountry();
  942. $data['kursdata'] = $kursdata;
  943. $data['state'] = $this->customer_model->getState($data['data'][0]->shipping_country_id);
  944. $data['city'] = $this->customer_model->getCity($data['data'][0]->shipping_state_id);
  945. $data['product'] = $this->sales_model->getProducts($data['data'][0]->warehouse_id);
  946. $data['items'] = $this->sales_model->getSalesItems($data['data'][0]->sales_id);
  947. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  948. $data['termin'] = $this->sales_model->getTerminSalesId($data['data'][0]->sales_id);
  949.  
  950. $this->load->view('sales/revisi', $data);
  951. }
  952.  
  953. /*
  954. this fucntion is to edit sales record and save in database
  955. */
  956.  
  957. public function editSales() {
  958. $id = $this->input->post('sales_id');
  959. $submit = $this->input->post('submit');
  960. if ($submit == "Update") {
  961. $status = 0;
  962. } else {
  963. $status = 4;
  964. }
  965.  
  966. $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  967. if ($this->form_validation->run() == false) {
  968. $this->edit($id);
  969. } else {
  970. $warehouse_id = $this->input->post('warehouse');
  971. $old_warehouse_id = $this->input->post('old_warehouse_id');
  972. $warehouse_change = $this->input->post('warehouse_change');
  973. $data = array(
  974. "currentdate" => $this->input->post('sales_date'),
  975. "date" => $this->input->post('cust_date'),
  976. "cc_holder" => $this->input->post('taxx'),
  977. "po_cust" => $this->input->post('po_cust'),
  978. "warehouse_id" => $this->input->post('warehouse'),
  979. "ketnama" => $this->input->post('reference'),
  980. "biller_id" => $this->input->post('biller_id'),
  981. "customer_id" => $this->input->post('customer'),
  982. "upcc" => $this->input->post('main'),
  983. "upcc2" => $this->input->post('cc'),
  984. "total" => $this->input->post('grand_total'),
  985. "discount_value" => $this->input->post('total_discount'),
  986. "tax_value" => $this->input->post('total_tax'),
  987. "note" => $this->input->post('note'),
  988. "shipping_city_id" => $this->input->post('city'),
  989. "shipping_state_id" => $this->input->post('state'),
  990. "shipping_country_id" => $this->input->post('country'),
  991. "shipping_address" => $this->input->post('address'),
  992. "shipping_charge" => $this->input->post('shipping_charge'),
  993. "internal_note" => $this->input->post('internal_note'),
  994. "mode_of_transport" => $this->input->post('mode_of_transport'),
  995. "transporter_name" => $this->input->post('transporter_name'),
  996. "transporter_code" => $this->input->post('transporter_code'),
  997. "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  998. "l_r_no" => $this->input->post('l_r_no'),
  999. "chalan_no" => $this->input->post('chalan_no'),
  1000. "indent_no" => $this->input->post('indent_no'),
  1001. "credit_days" => $this->input->post('credit_days'),
  1002. "date_of_supply" => $this->input->post('date_of_supply'),
  1003. "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  1004. "gst_payable" => $this->input->post('gst_payable'),
  1005. "broker" => $this->input->post('broker'),
  1006. "user" => $this->session->userdata('user_id'),
  1007. "term_and_condition" => $this->input->post('term_and_condition'),
  1008. "sales_id" => $this->input->post('sales_id'),
  1009. "user_name" => $this->session->userdata('identity'),
  1010. "kurs_id" => $this->input->post('kursdata'),
  1011. "nominalcurrency" => $this->input->post('grand_currency'),
  1012. "ATTN" => $this->input->post('tax'),
  1013. "shipto" => $this->input->post('shipto'),
  1014. "billto" => $this->input->post('billto'),
  1015. "dateestimate" => $this->input->post('dateestimate'),
  1016. "salesdesc" => $this->input->post('others'),
  1017. "paid_by" => $this->input->post('paymenterms') ?: 'advance',
  1018. "additional_description" => $this->input->post('additional_description'),
  1019. "service_description" => $this->input->post('service_description'),
  1020. "diskonpersen" => $this->input->post('DiscountPersen'),
  1021. "diskon" => $this->input->post('diskonfix'),
  1022. "status_rev" => $status
  1023. );
  1024.  
  1025. if ($this->sales_model->editModel($id, $data)) {
  1026. $log_data = array(
  1027. 'user_id' => $this->session->userdata('user_id'),
  1028. 'table_id' => $id,
  1029. 'message' => 'Sales Updated'
  1030. );
  1031. $this->log_model->insert_log($log_data);
  1032.  
  1033.  
  1034. $this->sales_model->deleteTerminModel($id);
  1035.  
  1036. $jumlahtermin = $this->input->post('terminnumber');
  1037.  
  1038. if (!empty($jumlahtermin)) {
  1039. $termins = $this->input->post('terminvalue');
  1040. $tanggaltermins = $this->input->post('tanggalterminvalue');
  1041. $ket = $this->input->post('terminket');
  1042. if (!empty($termins)) {
  1043. $im = 0;
  1044. $i = 1;
  1045. foreach ($termins as $quan) {
  1046. $value = $this->input->post('grand_total');
  1047. $nominal = ($value * $quan) / 100;
  1048. $nominal = number_format((float) $nominal, 2, '.', '');
  1049. if (!empty($tanggaltermins[$im])) {
  1050. $datetermin = $tanggaltermins[$im];
  1051. } else {
  1052. $datetermin = date('Y-m-d');
  1053. }
  1054.  
  1055. $datatermin = array(
  1056. "sales_id" => $id,
  1057. "persen" => $quan,
  1058. "nominal" => $nominal,
  1059. "tanggalbayar" => $datetermin,
  1060. "keterangan" => $ket[$i]
  1061. );
  1062.  
  1063. $this->sales_model->addTerminModel($datatermin);
  1064. $i++;
  1065. $im++;
  1066. }
  1067. } else {
  1068.  
  1069. }
  1070. }
  1071.  
  1072.  
  1073. // $this->sales_model->deleteSalesItemsAll($id);
  1074.  
  1075. // $product_id = $this->input->post('product_id');
  1076. // $quantity = $this->input->post('qty');
  1077. // $price = $this->input->post('price');
  1078. // $gross_total = $this->input->post('linetotal');
  1079. // $discount_id = $this->input->post('discount_id');
  1080. // $discount_value = $this->input->post('discount_value');
  1081. // $discount = $this->input->post('discount');
  1082. // $serial_no = $this->input->post('serial_no');
  1083. // $sales_id = $id;
  1084.  
  1085. // foreach ($product_id as $e => $pd) {
  1086.  
  1087. // $a['product_id'] = $pd;
  1088. // $a['quantity'] = $quantity[$e];
  1089. // $a['price'] = $price[$e];
  1090. // $a['gross_total'] = $gross_total[$e];
  1091. // $a['discount_id'] = $discount_id[$e];
  1092. // $a['discount_value'] = $discount_value[$e];
  1093. // $a['discount'] = $discount[$e];
  1094. // $a['serial_no'] = $serial_no[$e];
  1095. // $a['sales_id'] = $sales_id;
  1096.  
  1097. // $this->db->insert('sales_items', $a);
  1098.  
  1099. // }
  1100.  
  1101. $js_data = json_decode($this->input->post('table_data'));
  1102. $js_data1 = json_decode($this->input->post('tableadditional_data'));
  1103. $js_data2 = json_decode($this->input->post('tableservice_data'));
  1104.  
  1105. $this->sales_model->deleteSalesItemsAll($id);
  1106.  
  1107. $id_product = $this->input->post('idProduct');
  1108. $quantity = $this->input->post('quantitys');
  1109. $price = $this->input->post('prices');
  1110. $total = $this->input->post('totals');
  1111. // $discount_id = $this->input->post('hidden_discount');
  1112. $discount_value = $this->input->post('discount_values');
  1113. // $serial_no =
  1114. $sales_id = $id;
  1115.  
  1116. $patokan = $this->input->post('patokan');
  1117. // var_dump($id_product);
  1118. // die;
  1119.  
  1120. foreach ($patokan as $e => $pd) {
  1121. // var_dump($pd);
  1122. // die;
  1123. $a['product_id'] = $this->input->post('idProduct'.$pd);
  1124. // var_dump($a['product_id']);
  1125. // die;
  1126. // $a['quantity'] = $quantity[$e];
  1127. // $a['price'] = $price[$e];
  1128. // $a['gross_total'] = $total[$e];
  1129. // // $a['discount_id'] = $discount_id[$e];
  1130. // $a['discount_value'] = $discount_value[$e];
  1131. // $a['sales_id'] = $id;
  1132.  
  1133. $a['quantity'] = $this->input->post('qty'.$pd);
  1134. $a['price'] = $this->input->post('price'.$pd);
  1135. $a['gross_total'] = $this->input->post('linetotal'.$pd);
  1136. $a['serial_no'] = $this->input->post('description'.$pd);
  1137. $a['discount_id'] = $this->input->post('item_discount'.$pd);
  1138.  
  1139. if($a['discount_id'] != ''){
  1140. $dk = $this->db->where('discount_id = ',$a['discount_id'])->get('discount')->row();
  1141. $diskon = $dk->discount_value / 100 * $a['price'];
  1142.  
  1143. $a['discount_value'] = $dk->discount_value;
  1144. $a['discount'] = $diskon;
  1145. }
  1146.  
  1147. $a['sales_id'] = $id;
  1148.  
  1149. $this->db->insert('sales_items', $a);
  1150. }
  1151.  
  1152. // foreach ($js_data as $key => $value) {
  1153. // if (!empty($value) and ! empty($value->product_id)) {
  1154.  
  1155. // $data = array(
  1156. // "product_id" => $value->product_id,
  1157. // "quantity" => (!empty($value->quantity)) ? $value->quantity : 0,
  1158. // "price" => (!empty($value->price)) ? $value->price : 0,
  1159. // "gross_total" => (!empty($value->total)) ? $value->total : 0,
  1160. // "discount_id" => (!empty($value->discount_id)) ? $value->discount_id : 0,
  1161. // "discount_value" => (!empty($value->discount_value)) ? $value->discount_value : 0,
  1162. // "discount" => (!empty($value->discount)) ? $value->discount : 0,
  1163.  
  1164. // "serial_no" => (!empty($value->desc)) ? $value->desc : 0,
  1165. // "sales_id" => $id
  1166. // );
  1167.  
  1168. // $this->db->insert('sales_items', $data);
  1169. // }
  1170. // }
  1171.  
  1172. $this->sales_model->deleteSalesAdditionalAll($id);
  1173. foreach ($js_data1 as $key => $value) {
  1174. if ($value != null || !empty($value->additional_id)) {
  1175.  
  1176. $data = array(
  1177. "product_id" => $value->additional_id,
  1178. "quantity" => $value->quantity,
  1179. "price" => $value->price,
  1180. "gross_total" => $value->total,
  1181. "discount_id" => @$value->discount_id,
  1182. "discount_value" => @$value->discount_value,
  1183. "discount" => $value->discount,
  1184. "desc" => $value->desc,
  1185. "sales_id" => $id
  1186. );
  1187.  
  1188. $this->db->insert('sales_additional_items' ,$data);
  1189. }
  1190. }
  1191.  
  1192. $this->sales_model->deleteSalesServiceItemsAll($id);
  1193. if (!empty($js_data2)) {
  1194. foreach ($js_data2 as $key => $value) {
  1195. if ($value != null || !empty($value->service_id)) {
  1196.  
  1197. $data = array(
  1198. "service_id" => $value->service_id,
  1199. "quantity" => $value->quantity,
  1200. "price" => $value->price,
  1201. "gross_total" => $value->total,
  1202. "discount_id" => @$value->discount_id,
  1203. "discount_value" => @$value->discount_value,
  1204. "discount" => $value->discount,
  1205. "sales_id" => $id
  1206. );
  1207.  
  1208. $this->sales_model->addSalesServiceItem($data);
  1209. }
  1210. }
  1211. }
  1212.  
  1213. redirect('sales/view/' . $id);
  1214. } else {
  1215. redirect('sales', 'refresh');
  1216. }
  1217. }
  1218. }
  1219.  
  1220. /*
  1221. this function is used to delete sales record from database
  1222. */
  1223.  
  1224. public function delete($id) {
  1225. if ($this->sales_model->deleteModel($id)) {
  1226. $log_data = array(
  1227. 'user_id' => $this->session->userdata('user_id'),
  1228. 'table_id' => $id,
  1229. 'message' => 'Sales Deleted'
  1230. );
  1231. $this->log_model->insert_log($log_data);
  1232. redirect('sales', 'refresh');
  1233. } else {
  1234. redirect('sales', 'refresh');
  1235. }
  1236. }
  1237.  
  1238. /*
  1239. display data in dashboard calendar
  1240. */
  1241.  
  1242. public function calendar() {
  1243. log_message('debug', print_r($this->db->get('category')->result(), true));
  1244. $data = $this->sales_model->getCalendarData();
  1245. $total = 0;
  1246. foreach ($data as $value) {
  1247. $date = Date('Y-m-d');
  1248. if ($date == $value->date) {
  1249. $total += $value->total;
  1250. }
  1251. $temp = array(
  1252. "title" => $total,
  1253. "start" => "2017-04-05T00:01:00+05:30"
  1254. );
  1255. }
  1256. echo json_encode($temp);
  1257. }
  1258.  
  1259. /*
  1260. view Sales details
  1261. */
  1262.  
  1263. public function view($id) {
  1264. $data['quotation'] = $this->quotation_model->getQuotation();
  1265. $data['data'] = $this->sales_model->getDetails($id);
  1266. $data['items'] = $this->sales_model->getItems($id);
  1267. $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1268.  
  1269. for ($i=0; $i < count($data['items']) ; $i++) {
  1270.  
  1271. if ($data['items'][$i]->product_grouping != NULL) {
  1272. $arlist = json_encode(unserialize($data['items'][$i]->list));
  1273. $arlist = json_decode($arlist);
  1274. $max = array();
  1275. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1276.  
  1277. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  1278. $count = ($qtyp / intval($arlist[$ilist]->ql));
  1279. array_push($max, $count);
  1280. }
  1281. $min = min($max);
  1282. $data['items'][$i]->quantity_product = intval($min);
  1283. }
  1284. }
  1285. $data['additional'] = $this->sales_model->getAdditional($id);
  1286. for ($i=0; $i < count($data['additional']) ; $i++) {
  1287.  
  1288. if ($data['additional'][$i]->product_grouping != NULL) {
  1289. $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1290. $arlist = json_decode($arlist);
  1291. $max = array();
  1292. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1293.  
  1294. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  1295. $count = ($qtyp / intval($arlist[$ilist]->ql));
  1296. array_push($max, $count);
  1297. }
  1298. $min = min($max);
  1299. $data['additional'][$i]->quantity_product = intval($min);
  1300. }
  1301. }
  1302. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1303. $data['company'] = $this->purchase_model->getCompany();
  1304. $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1305. $this->load->view('sales/view', $data);
  1306. }
  1307.  
  1308. /*
  1309. generate pdf
  1310. */
  1311.  
  1312. public function pdf($id) {
  1313. $log_data = array(
  1314. 'user_id' => $this->session->userdata('user_id'),
  1315. 'table_id' => $id,
  1316. 'message' => 'Invoice Generated'
  1317. );
  1318. $this->log_model->insert_log($log_data);
  1319. ob_start();
  1320. $html = ob_get_clean();
  1321. $html = utf8_encode($html);
  1322. $data['quotation'] = $this->quotation_model->getQuotation();
  1323. $data['data'] = $this->sales_model->getDetails($id);
  1324. $data['items'] = $this->sales_model->getItems($id);
  1325. for ($i=0; $i < count($data['items']) ; $i++) {
  1326.  
  1327. if ($data['items'][$i]->product_grouping != NULL) {
  1328. $arlist = json_encode(unserialize($data['items'][$i]->list));
  1329. $arlist = json_decode($arlist);
  1330. $array = array();
  1331. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1332.  
  1333. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1334. array_push($array, $name);
  1335. }
  1336. $list = implode(', ', $array);
  1337.  
  1338. $data['items'][$i]->name = $data['items'][$i]->name. ' : ' .$list;
  1339. }
  1340. }
  1341. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1342. $data['company'] = $this->purchase_model->getCompany();
  1343. $data['company_setting'] = $this->company_setting_model->getData();
  1344. $data['signature'] = $this->signature_model->getActiveSignature();
  1345. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1346. $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1347. $data['additional'] = $this->sales_model->getAdditional($id);
  1348. $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1349. for ($i=0; $i < count($data['additional']) ; $i++) {
  1350.  
  1351. if ($data['additional'][$i]->product_grouping != NULL) {
  1352. $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1353. $arlist = json_decode($arlist);
  1354. $array = array();
  1355. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1356.  
  1357. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1358. array_push($array, $name);
  1359. }
  1360. $list = implode(', ', $array);
  1361.  
  1362. $data['additional'][$i]->name = $data['additional'][$i]->name. ' : ' .$list;
  1363. }
  1364. }
  1365. $data['bank'] = $this->bank_model->getRecordAr();
  1366. $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1367. $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1368.  
  1369. // UNTUK FONT
  1370. $t = 0;
  1371. foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1372. foreach ($data['additional'] as $value) { $t += $value->gross_total; }
  1373. foreach ($data['serviceitems'] as $value) { $t += ($value->price * $value->quantity); }
  1374.  
  1375. $r = $t - $data['data'][0]->discount_value + $data['data'][0]->shipping_charge + $data['data'][0]->tax_value;
  1376. if ($r > $t) {
  1377. $font = $r;
  1378. } else {
  1379. $font = $t;
  1380. }
  1381.  
  1382. if(strlen($font) <= 9) {
  1383. $data['style'] = 'style="font-size: 9pt"';
  1384. $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1385. } elseif (strlen($font) == 10) {
  1386. $data['style'] = 'style="font-size: 8pt"';
  1387. $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1388. } elseif (strlen($font) >= 11) {
  1389. $data['style'] = 'style="font-size: 7pt"';
  1390. $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1391. }
  1392.  
  1393. $html = $this->load->view('sales/pdf', $data, true);
  1394.  
  1395. $logo = base_url(header_logo('logo'));
  1396. $width = header_logo('width');
  1397. $height = header_logo('height');
  1398.  
  1399. $mpdf = new \Mpdf\Mpdf(['setAutoTopMargin' => 'stretch', 'format' => [216, 356]]);
  1400. $mpdf->SetHTMLHeader('
  1401.  
  1402. <table style=" vertical-align: bottom; font-family: serif; font-size: 9pt;" width="100%">
  1403. <tr>
  1404. <td width="40%" valign="top"><img src="'. $logo .'" width="'. $width .'" height="'. $height .'"></td>
  1405. <td style="text-align: right;" width="60%">
  1406. '.header_doc() . '
  1407.  
  1408. </td>
  1409. </tr>
  1410. </table>');
  1411. if ($data['data'][0]->status_rev == 4) {
  1412. $status = 'Draft';
  1413. $mpdf->SetWatermarkText($status);
  1414. $mpdf->showWatermarkText = true;
  1415. } else {
  1416. $mpdf->SetWatermarkImage(base_url('/assets/images/logo-glodisa.jpg'));
  1417. $mpdf->showWatermarkImage = true;
  1418. }
  1419. $mpdf->allow_charset_conversion = true;
  1420. $mpdf->setFooter('|page {PAGENO} of {nbpg}|');
  1421. $mpdf->charset_in = 'UTF-8';
  1422. $mpdf->use_kwt = true;
  1423. $mpdf->WriteHTML($html);
  1424. $mpdf->Output(($data['data'][0]->po_new ?: $data['data'][0]->po) . '.pdf', 'I');
  1425. }
  1426.  
  1427. public function word($id, $ida='1') {
  1428. $idar = str_replace('_', ',', $ida);
  1429. $log_data = array(
  1430. 'user_id' => $this->session->userdata('user_id'),
  1431. 'table_id' => $id,
  1432. 'message' => 'Invoice Generated'
  1433. );
  1434. $this->log_model->insert_log($log_data);
  1435. ob_start();
  1436. $html = ob_get_clean();
  1437. $html = utf8_encode($html);
  1438. $data['quotation'] = $this->quotation_model->getQuotation();
  1439. $data['data'] = $this->sales_model->getDetails($id);
  1440. $data['items'] = $this->sales_model->getItems($id);
  1441. for ($i=0; $i < count($data['items']) ; $i++) {
  1442.  
  1443. if ($data['items'][$i]->product_grouping != NULL) {
  1444. $arlist = json_encode(unserialize($data['items'][$i]->list));
  1445. $arlist = json_decode($arlist);
  1446. $array = array();
  1447. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1448.  
  1449. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1450. array_push($array, $name);
  1451. }
  1452. $list = implode(', ', $array);
  1453.  
  1454. $data['items'][$i]->name = $data['items'][$i]->name. ' : ' .$list;
  1455. }
  1456. }
  1457. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1458. $data['company'] = $this->purchase_model->getCompany();
  1459. $data['company_setting'] = $this->company_setting_model->getData();
  1460. $data['signature'] = $this->signature_model->getActiveSignature();
  1461. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1462. $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1463. $data['additional'] = $this->sales_model->getAdditional($id);
  1464. $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1465. for ($i=0; $i < count($data['additional']) ; $i++) {
  1466.  
  1467. if ($data['additional'][$i]->product_grouping != NULL) {
  1468. $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1469. $arlist = json_decode($arlist);
  1470. $array = array();
  1471. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1472.  
  1473. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1474. array_push($array, $name);
  1475. }
  1476. $list = implode(', ', $array);
  1477.  
  1478. $data['additional'][$i]->name = $data['additional'][$i]->name. ' : ' .$list;
  1479. }
  1480. }
  1481. $data['bank'] = $this->bank_model->getRecordAr();
  1482. $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1483. $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1484.  
  1485. // UNTUK FONT
  1486. $t = 0;
  1487. foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1488. foreach ($data['additional'] as $value) { $t += $value->gross_total; }
  1489. foreach ($data['serviceitems'] as $value) { $t += ($value->price * $value->quantity); }
  1490.  
  1491. $r = $t - $data['data'][0]->discount_value + $data['data'][0]->shipping_charge + $data['data'][0]->tax_value;
  1492. if ($r > $t) {
  1493. $font = $r;
  1494. } else {
  1495. $font = $t;
  1496. }
  1497.  
  1498. if(strlen($font) <= 9) {
  1499. $data['style'] = 'style="font-size: 9pt"';
  1500. $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1501. } elseif (strlen($font) == 10) {
  1502. $data['style'] = 'style="font-size: 8pt"';
  1503. $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1504. } elseif (strlen($font) >= 11) {
  1505. $data['style'] = 'style="font-size: 7pt"';
  1506. $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1507. }
  1508.  
  1509. $this->load->library('lmsword');
  1510. $this->lmsword->page_setup('A4');
  1511. $this->lmsword->html($this->load->view('sales/pdf', $data, true));
  1512. $this->lmsword->create($data['data'][0]->po_new ?: $data['data'][0]->po);
  1513. }
  1514.  
  1515. public function print1($id, $idb='1') {
  1516. $idar = str_replace('_', ',', $idb);
  1517. $log_data = array(
  1518. 'user_id' => $this->session->userdata('user_id'),
  1519. 'table_id' => $id,
  1520. 'message' => 'Invoice Printed'
  1521. );
  1522. $this->log_model->insert_log($log_data);
  1523. $data['data'] = $this->sales_model->getDetails($id);
  1524. $data['items'] = $this->sales_model->getItems($id);
  1525. $data['company'] = $this->purchase_model->getCompany();
  1526. $data['quotation'] = $this->quotation_model->getQuotation();
  1527. $data['data'] = $this->sales_model->getDetails($id);
  1528. $data['bank'] = $this->bank_model->getRecordAr($idar);
  1529. $data['additional'] = $this->sales_model->getAdditional($id);
  1530. $data['items'] = $this->sales_model->getItems($id);
  1531. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1532. $data['company'] = $this->purchase_model->getCompany();
  1533. $data['company_setting'] = $this->company_setting_model->getData();
  1534. $data['signature'] = $this->signature_model->getActiveSignature();
  1535. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1536. $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1537. $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1538. $this->load->view('sales/pdf', $data);
  1539. }
  1540.  
  1541. /*
  1542. send email
  1543. */
  1544.  
  1545. public function email($id) {
  1546. $log_data = array(
  1547. 'user_id' => $this->session->userdata('user_id'),
  1548. 'table_id' => 0,
  1549. 'message' => 'Invoice Email Send'
  1550. );
  1551. $this->log_model->insert_log($log_data);
  1552. $email = $this->sales_model->getSmtpSetup();
  1553.  
  1554. $data = $this->sales_model->getCustomerEmail($id);
  1555. $company = $this->purchase_model->getCompany();
  1556. $this->load->view('class.phpmailer.php');
  1557.  
  1558. $mail = new PHPMailer();
  1559.  
  1560. $mail->IsSMTP();
  1561. $mail->Host = $email->smtp_host;
  1562.  
  1563. $mail->SMTPAuth = true;
  1564. //$mail->SMTPSecure = "ssl";
  1565. $mail->Port = $email->smtp_port;
  1566. $mail->Username = $email->smtp_username;
  1567. $mail->Password = $email->smtp_password;
  1568.  
  1569. $mail->From = $email->from_address;
  1570. $mail->FromName = $email->from_name;
  1571. $mail->AddAddress($data[0]->email);
  1572. //$mail->AddReplyTo("mail@mail.com");
  1573.  
  1574. /* $mail->IsHTML(true);
  1575.  
  1576. $mail->Subject = "Purchase order No : ".$data[0]->reference_no." From ".$company[0]->name;
  1577. $mail->Body = "Date : ".$data[0]->date."<br>Total : ".$data[0]->total;
  1578. //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
  1579.  
  1580. if(!$mail->Send())
  1581. {
  1582. $message = "Email could not be sent";
  1583. }
  1584. else{
  1585. $message = "Email has been sent";
  1586. } */
  1587. $total = $data[0]->total + $data[0]->shipping_charge;
  1588. $this->load->library('email');
  1589.  
  1590. $this->email->from($email->from_address, $email->from_name);
  1591. $this->email->to($data[0]->email);
  1592. $this->email->subject("Sales order No : " . $data[0]->reference_no . " From " . $company[0]->name);
  1593. $this->email->message("Date : " . $data[0]->date . " \nTotal : " . $total . " \n\n\nComapany Name : " . $company[0]->name . "\nAddress : " . $company[0]->street . " " . $company[0]->country_name . "\nMobile No :" . $company[0]->phone);
  1594. //Send mail
  1595. if ($this->email->send())
  1596. $message = "Email sent successfully.";
  1597. else
  1598. $message = "Error in sending Email.";
  1599.  
  1600. $this->session->set_flashdata('message', $message);
  1601. redirect('sales', 'refresh');
  1602. }
  1603.  
  1604. /*
  1605. payment view
  1606. */
  1607.  
  1608. public function payment($id) {
  1609. $data['data'] = $this->sales_model->getDetailsPayment($id);
  1610. $data['company'] = $this->purchase_model->getCompany();
  1611. $data['p_reference_no'] = $this->sales_model->generateReferenceNo();
  1612.  
  1613. $this->load->view('sales/payment', $data);
  1614. }
  1615.  
  1616. /*
  1617. get payment details to view and send to model
  1618. */
  1619.  
  1620. public function addPayment() {
  1621. $id = $this->input->post('id');
  1622. $paying_by = $this->input->post('paying_by');
  1623. $this->form_validation->set_rules('date', 'Date', 'trim|required');
  1624. $this->form_validation->set_rules('paying_by', 'Paying By', 'trim|required');
  1625. if ($paying_by == "Cheque") {
  1626. $this->form_validation->set_rules('bank_name', 'Bank Name', 'trim|required|callback_alpha_dash_space');
  1627. $this->form_validation->set_rules('cheque_no', 'Cheque No', 'trim|required|numeric');
  1628. }
  1629. if ($this->form_validation->run() == false) {
  1630. $this->payment($id);
  1631. } else {
  1632. if ($paying_by == "Cheque") {
  1633. $bank_name = $this->input->post('bank_name');
  1634. $cheque_no = $this->input->post('cheque_no');
  1635. } else {
  1636. $bank_name = "";
  1637. $cheque_no = "";
  1638. }
  1639. $data = array(
  1640. "sales_id" => $id,
  1641. "date" => $this->input->post('date'),
  1642. "reference_no" => $this->input->post('reference_no'),
  1643. "amount" => $this->input->post('amount'),
  1644. "paying_by" => $this->input->post('paying_by'),
  1645. "bank_name" => $bank_name,
  1646. "cheque_no" => $cheque_no,
  1647. "description" => $this->input->post('note')
  1648. );
  1649.  
  1650. if ($this->sales_model->addPayment($data)) {
  1651. $log_data = array(
  1652. 'user_id' => $this->session->userdata('user_id'),
  1653. 'table_id' => $id,
  1654. 'message' => 'Sales Payable'
  1655. );
  1656. $this->log_model->insert_log($log_data);
  1657. redirect('sales', 'refresh');
  1658. } else {
  1659. redirect("sales", 'refresh');
  1660. }
  1661. }
  1662. }
  1663.  
  1664. /*
  1665. generate invoice
  1666. */
  1667.  
  1668. public function invoice() {
  1669. $data['data'] = $this->sales_model->invoice();
  1670. $this->load->view('invoice/list', $data);
  1671. }
  1672.  
  1673. /*
  1674.  
  1675. */
  1676.  
  1677. public function getCustomerData($id) {
  1678. $data['data'] = $this->customer_model->getRecord($id);
  1679. $data['country'] = $this->customer_model->getCountry();
  1680. $data['state'] = $this->customer_model->getState($data['data'][0]->country_id);
  1681. $data['city'] = $this->customer_model->getCity($data['data'][0]->state_id);
  1682. $data['address'] = $this->customer_model->getListAddressRecord($id);
  1683. echo json_encode($data);
  1684. }
  1685.  
  1686. public function getRecordAjax($id) {
  1687. $data['data'] = $this->quotation_model->getRecordAjax($id);
  1688. echo json_encode($data);
  1689. }
  1690.  
  1691. public function getCustomerAddressData($id) {
  1692. $data['data'] = $this->customer_model->getRecord($id);
  1693. $data['address'] = $this->customer_model->getCustomerAddressbyID($id);
  1694. echo json_encode($data);
  1695. }
  1696.  
  1697. /*
  1698. check character and space validation
  1699. */
  1700.  
  1701. function alpha_dash_space($str) {
  1702. if (!preg_match("/^([a-zA-Z ])+$/i", $str)) {
  1703. $this->form_validation->set_message('alpha_dash_space', 'The %s field may only contain alpha and spaces');
  1704. return FALSE;
  1705. } else {
  1706. return TRUE;
  1707. }
  1708. }
  1709.  
  1710. function currencyConverter2($currency_from, $currency_to) {
  1711. if ($currency_from == $currency_to) {
  1712. return 1;
  1713. } else {
  1714. //$currency_output = round(currency($currency_input, $currency_from,$currency_to, false),2);
  1715. //$currency_input=substr($currency_input, 0, -3);
  1716. $currency_input = filter_var(1, FILTER_SANITIZE_NUMBER_INT);
  1717. $from_Currency = urlencode($currency_from);
  1718. $to_Currency = urlencode($currency_to);
  1719. $get = @file_get_contents("https://finance.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency");
  1720. $get = @explode("<span class=bld>", $get);
  1721. $get = @explode("</span>", @$get[1]);
  1722. $converted_currency = preg_replace("/[^0-9\.]/", null, @$get[0]);
  1723. $converted_currency = $converted_currency;
  1724. return $converted_currency;
  1725.  
  1726. //return $currency_output;
  1727. }
  1728. }
  1729.  
  1730. }
  1731.  
  1732. ?>
RAW Paste Data