新版订单消耗系统

CustomerInvoices.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class CustomerInvoices extends Model
  6. {
  7. protected $table = 'customer_invoices';
  8. public $timestamps = false;
  9. protected static $unguarded = true;
  10. public static $typeList = [1,3,4,5,6];
  11. public static function invoiceHistoryOld($orderIds)
  12. {
  13. $details = CustomerInvoiceOrders::select('inv_id', 'order_id', 'invoice_amount')
  14. ->whereIn('order_id', $orderIds)->where('enable', 1)
  15. ->orderBy('id', 'asc')->get();
  16. $orders = Order::select('id', 'task_id', 'customer_name', 'billing_date', 'final_amount')
  17. ->whereIn('id', $orderIds)->where('enable', 1)
  18. ->get()->keyBy('id')->toArray();
  19. $inv_ids = $details->pluck('inv_id');
  20. $inv_ids = json_decode(json_encode($inv_ids), 1);
  21. $inv_ids = array_unique($inv_ids);
  22. $typeList = self::$typeList;
  23. if (empty($inv_ids)) {
  24. $invoices = [];
  25. } else {
  26. $sql = 'select ci.* from customer_invoices as ci left join statements as s on s.id = ci.statements_id where
  27. s.type in ('.implode(',', $typeList).') and ci.status = 4 and ci.is_invoiced=1 and ci.id in ('.implode(',', $inv_ids) . ') and ci.enable = 1
  28. and s.enable = 1';
  29. $invoices = \DB::select($sql);
  30. $invoices = json_decode(json_encode($invoices), 1);
  31. $invoices = array_column($invoices, null, 'id');
  32. }
  33. $data = array();
  34. foreach($details->toArray() as $k=>&$v){
  35. $inv_id = $v['inv_id'];
  36. if(!isset($invoices[$inv_id])){
  37. continue;
  38. }
  39. $invoice = $invoices[$inv_id];
  40. $order = $orders[$v['order_id']];
  41. if(!isset($data[$v['order_id']])) {
  42. //第一次开票
  43. $data[$v['order_id']] = [
  44. 'first_invoice_amount' => $v['invoice_amount'],
  45. 'first_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  46. 'invoice_num' => 1,
  47. 'second_invoice_amount' => 0,
  48. 'second_invoice_date' => null,
  49. 'uninvoiced_amount' => $order['final_amount'] - $v['invoice_amount'],
  50. 'invoice_amount' => $v['invoice_amount'],
  51. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  52. ];
  53. } else {
  54. //第二次开票
  55. $data[$v['order_id']] = [
  56. 'invoice_num' => 2,
  57. 'first_invoice_amount' => $data[$v['order_id']]['first_invoice_amount'],
  58. 'first_invoice_date' => $data[$v['order_id']]['first_invoice_date'],
  59. 'second_invoice_amount' => $v['invoice_amount'],
  60. 'second_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  61. 'uninvoiced_amount' => $data[$v['order_id']]['uninvoiced_amount'] - $v['invoice_amount'],
  62. 'invoice_amount' => ($data[$v['order_id']]['first_invoice_amount'] + $v['invoice_amount']),
  63. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  64. ];
  65. }
  66. // $data[] = [
  67. // 'task_id' => $order['task_id'],
  68. // 'invoice_amount' => $v['invoice_amount'],
  69. // 'cdate' => $order['billing_date'],
  70. // 'created_at' => $invoice['created_at']
  71. // ];
  72. }
  73. return $data;
  74. }
  75. public static function invoiceHistory($orderIds)
  76. {
  77. $details = CustomerInvoiceOrders::select('inv_id', 'order_id', 'invoice_amount')
  78. ->whereIn('order_id', $orderIds)->where('enable', 1)
  79. ->orderBy('id', 'asc')->get();
  80. $orders = Order::select('id', 'task_id', 'customer_name', 'billing_date', 'final_amount')
  81. ->whereIn('id', $orderIds)->where('enable', 1)
  82. ->get()->keyBy('id')->toArray();
  83. $inv_ids = $details->pluck('inv_id');
  84. $inv_ids = json_decode(json_encode($inv_ids), 1);
  85. $inv_ids = array_unique($inv_ids);
  86. $typeList = self::$typeList;
  87. if (empty($inv_ids)) {
  88. $invoices = [];
  89. } else {
  90. $sql = 'select ci.* from customer_invoices as ci left join statements as s on s.id = ci.statements_id where
  91. s.type in ('.implode(',', $typeList).') and ci.status = 4 and ci.is_invoiced=1 and ci.id in ('.implode(',', $inv_ids) . ') and ci.enable = 1
  92. and s.enable = 1';
  93. $invoices = \DB::select($sql);
  94. $invoices = json_decode(json_encode($invoices), 1);
  95. $invoices = array_column($invoices, null, 'id');
  96. }
  97. $data = array();
  98. foreach($details->toArray() as $k=>&$v){
  99. $inv_id = $v['inv_id'];
  100. if(!isset($invoices[$inv_id])){
  101. continue;
  102. }
  103. $invoice = $invoices[$inv_id];
  104. $order = $orders[$v['order_id']];
  105. if(!isset($data[$v['order_id']])) {
  106. //第一次开票
  107. $data[$v['order_id']] = [
  108. 'first_invoice_amount' => $v['invoice_amount'],
  109. 'first_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  110. 'invoice_num' => 1,
  111. 'second_invoice_amount' => 0,
  112. 'second_invoice_date' => null,
  113. 'uninvoiced_amount' => $order['final_amount'] - $v['invoice_amount'],
  114. 'invoice_amount' => $v['invoice_amount'],
  115. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  116. 'invoice_id' => !empty($invoice['id']) ? $invoice['id'] : null,
  117. 'collection_remark' => !empty($invoice['collection_remark']) ? $invoice['collection_remark'] : null,
  118. ];
  119. } else {
  120. //第二次开票
  121. $data[$v['order_id']] = [
  122. 'invoice_num' => 2,
  123. 'first_invoice_amount' => $data[$v['order_id']]['first_invoice_amount'],
  124. 'first_invoice_date' => $data[$v['order_id']]['first_invoice_date'],
  125. 'second_invoice_amount' => $v['invoice_amount'],
  126. 'second_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  127. 'uninvoiced_amount' => $data[$v['order_id']]['uninvoiced_amount'] - $v['invoice_amount'],
  128. 'invoice_amount' => ($data[$v['order_id']]['first_invoice_amount'] + $v['invoice_amount']),
  129. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  130. 'invoice_id' => !empty($invoice['id']) ? $invoice['id'] : null,
  131. 'collection_remark' => !empty($invoice['collection_remark']) ? $invoice['collection_remark'] : null,
  132. ];
  133. }
  134. }
  135. return $data;
  136. }
  137. public static function mcnInvoiceHistory($orderIds) {
  138. $details = CustomerInvoiceOrders::select('inv_id', 'order_id', 'invoice_amount')
  139. ->whereIn('order_id', $orderIds)->where('enable', 1)
  140. ->orderBy('id', 'asc')->get();
  141. $inv_ids = $details->pluck('inv_id');
  142. $inv_ids = json_decode(json_encode($inv_ids), 1);
  143. $inv_ids = array_unique($inv_ids);
  144. if (empty($inv_ids)) {
  145. $invoices = [];
  146. } else {
  147. $sql = 'select ci.* from customer_invoices as ci left join statements as s on s.id = ci.statements_id where
  148. s.type =2 and ci.status = 4 and ci.is_invoiced=1 and ci.id in ('.implode(',', $inv_ids) . ') and ci.enable = 1
  149. and s.enable = 1';
  150. $invoices = \DB::select($sql);
  151. $invoices = json_decode(json_encode($invoices), 1);
  152. $invoices = array_column($invoices, null, 'id');
  153. }
  154. $data = array();
  155. foreach($details->toArray() as $k=>&$v){
  156. $inv_id = $v['inv_id'];
  157. if(!isset($invoices[$inv_id])){
  158. continue;
  159. }
  160. $invoice = $invoices[$inv_id];
  161. if(!isset($data[$v['order_id']])) {
  162. //第一次开票
  163. $data[$v['order_id']] = [
  164. 'mcn_invoice_id' => !empty($invoice['id']) ? $invoice['id'] : null,
  165. 'mcn_collection_remark' => !empty($invoice['collection_remark']) ? $invoice['collection_remark'] : null,
  166. ];
  167. }
  168. }
  169. return $data;
  170. }
  171. public static function getOrderIdsByHandler($search) {
  172. # 根据月份以及经手人姓名获取初始订单id
  173. $orderIdList = Order::query()->where('handler', current($search['handler']))
  174. ->where(function($query) use ($search) {
  175. if(isset($search['month']) && $search['month']) {
  176. if(count($search['month']) == 1) {
  177. $query->where('month', '=', date('Y-m-01', strtotime(current($search['month'])) ) );
  178. } else {
  179. $monthList = array_map(function($value){
  180. return date('Y-m-01', strtotime($value));
  181. }, $search['month']);
  182. $query->whereIn('month', $monthList);
  183. }
  184. }
  185. })->where('enable', 1)->pluck('id')->toArray();
  186. # 根据订单id获取对应的发票id
  187. $customerInvoiceIdList = CustomerInvoiceOrders::where('enable', 1)
  188. ->whereIn('order_id', $orderIdList)->pluck('inv_id')->toArray();
  189. # 根据查询到的发票id获取已经成功开票的发票id
  190. $successCustomerInvoiceIdList = CustomerInvoices::join('statements', 'statements.id', 'customer_invoices.statements_id')->whereIn('customer_invoices.id', $customerInvoiceIdList)
  191. ->where('customer_invoices.enable', 1)->where('customer_invoices.is_invoiced', 1)->where('statements.type', '!=', 2)->pluck('customer_invoices.id')->toArray();
  192. # 根据成功开票的发票id再次查询对应的订单id
  193. $successOrderIdList = CustomerInvoiceOrders::where('enable', 1)
  194. ->whereIn('inv_id', $successCustomerInvoiceIdList)->pluck('order_id')->toArray();
  195. # 对订单id进行交集运算
  196. $actualOrderIdList = array_intersect($orderIdList, $successOrderIdList);
  197. return $actualOrderIdList;
  198. }
  199. public static function getOrderIdsByMcnHandler($search) {
  200. # 根据月份以及经手人姓名获取初始订单id
  201. $orderIdList = Order::where('month', $search['month'])->where('mcn_handler', current($search['mcn_handler']))
  202. ->where('enable', 1)->pluck('id')->toArray();
  203. # 根据订单id获取对应的发票id
  204. $customerInvoiceIdList = CustomerInvoiceOrders::where('enable', 1)
  205. ->whereIn('order_id', $orderIdList)->pluck('inv_id')->toArray();
  206. # 根据查询到的发票id获取已经成功开票的发票id
  207. $successCustomerInvoiceIdList = CustomerInvoices::join('statements', 'statements.id', 'customer_invoices.statements_id')->whereIn('customer_invoices.id', $customerInvoiceIdList)
  208. ->where('customer_invoices.enable', 1)->where('customer_invoices.is_invoiced', 1)->where('statements.type', 2)->pluck('customer_invoices.id')->toArray();
  209. # 根据成功开票的发票id再次查询对应的订单id
  210. $successOrderIdList = CustomerInvoiceOrders::where('enable', 1)
  211. ->whereIn('inv_id', $successCustomerInvoiceIdList)->pluck('order_id')->toArray();
  212. # 对订单id进行交集运算
  213. $actualOrderIdList = array_intersect($orderIdList, $successOrderIdList);
  214. return $actualOrderIdList;
  215. }
  216. public static function getSearchModel($search, $user) {
  217. $query = self::query();
  218. if (isset($search['id']) && !empty($search['id'])) {
  219. $query->where('id', $search['id']);
  220. }
  221. if (isset($search['statements_id']) && !empty($search['statements_id'])) {
  222. $query->where('statements_id', $search['statements_id']);
  223. }
  224. if (isset($search['status']) && is_numeric($search['status']) ){
  225. $query->where('status', $search['status'])->where('is_show', 1);
  226. }
  227. if( isset($search['customer_name']) && !empty($search['customer_name']) ){
  228. $query->where('customer_name', 'like', '%'.$search['customer_name'].'%');
  229. }
  230. if (isset($search['cdate']) && !empty($search['cdate'])) {
  231. $query->where('applicant_time', '>=', $search['cdate'].' 00:00:00');
  232. $query->where('applicant_time', '<=', $search['cdate'].' 23:59:59');
  233. }
  234. if (isset($search['handler_id']) && !empty($search['handler_id'])) {
  235. $query->where('handler_id', $search['handler_id']);
  236. }
  237. if (isset($search['company']) && !empty($search['company'])) {
  238. $query->where('company', $search['company']);
  239. }
  240. if (in_array($user->role_id, [10, 17, 18, 21])) {
  241. // 财务不看商务待审核的发票
  242. $query->where('status', '>=', 2);
  243. }
  244. if (21 == $user->role_id) {
  245. $query->where('company', 'like', '%海南%');
  246. }
  247. if (17 == $user->role_id) {
  248. $query->where('company', '上饶市合一科技有限公司');
  249. }
  250. if (isset($search['inv_list']) && is_array($search['inv_list']) && !empty($search['inv_list'])) {
  251. $query->whereIn('id', $search['inv_list']);
  252. }
  253. if (isset($search['approved_start_date']) && !empty($search['approved_start_date'])) {
  254. $query->where('confirm_verify_at', '>=', $search['approved_start_date'])->where('status', 4);
  255. }
  256. if (isset($search['approved_end_date']) && !empty($search['approved_end_date'])) {
  257. $query->where('confirm_verify_at', '<=', $search['approved_end_date'].'23:59:59')->where('status', 4);
  258. }
  259. if (isset($search['pre_audit_status']) && is_numeric($search['pre_audit_status'])) {
  260. $query->where('pre_audit_status', $search['pre_audit_status']);
  261. }
  262. if (isset($serach['reject_inv_list']) && !empty($search['reject_inv_list'])) {
  263. $query->whereIn('reject_inv_id', $search['reject_inv_list']);
  264. }
  265. if (isset($search['enable']) && is_numeric($search['enable'])) {
  266. $query->where('enable', 1);
  267. }
  268. return $query;
  269. }
  270. public static function getCustomerInvoiceList($search, $user, $offset, $pageSize) {
  271. $listQuery = self::getSearchModel($search, $user);
  272. $count = $listQuery->count();
  273. $list = $listQuery->select('id', 'customer_name', 'handler', 'created_at', 'status', 'applicant_time',
  274. 'is_invoiced', 'invoice_amount', 'invoiced_time', 'company', 'statements_id', 'voided_inv_id', 'date_of_collection',
  275. 'is_voided', 'is_show', 'verify_at', 'confirm_verify_at', 'pre_audit_status', 'pre_audit_content', 'collection_remark');
  276. if(in_array($user->role_id, [10, 17, 18, 21])) {
  277. $list->orderByRaw('is_show desc,if(`status`=2,0,1) asc,id desc');
  278. } else if (15 == $user->role_id){
  279. $list->where('is_show', 1)
  280. ->orderByRaw('if(`status`=1,0,1) asc,id desc');
  281. } else {
  282. $list->where('is_show', 1)
  283. ->orderBy('id', 'desc');
  284. }
  285. $list = $list->offset($offset)
  286. ->limit($pageSize)
  287. ->get();
  288. return [$list, $count];
  289. }
  290. public static function getCustomerInvoiceInfo($invoiceId) {
  291. return self::query()->where('id', $invoiceId)->where('enable', 1)->first();
  292. }
  293. public static function updateData($customerInvoiceId, $collectionData) {
  294. $res = self::query()->where('id', $customerInvoiceId)->update($collectionData);
  295. if($res) return 0;
  296. return 500;
  297. }
  298. public static function getNewRejectInvListBySearch($search, $user) {
  299. $listQuery = self::getSearchModel($search, $user);
  300. return $listQuery->get();
  301. }
  302. }