Advertisement
fadlyshafa

Untitled

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